<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>React基础 | 北城韩雨</title><meta name="keywords" content="React基础"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="React基础">
<meta property="og:type" content="article">
<meta property="og:title" content="React基础">
<meta property="og:url" content="http://example.com/2019/03/22/React%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="React基础">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f">
<meta property="article:published_time" content="2019-03-21T16:00:00.000Z">
<meta property="article:modified_time" content="2019-06-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="React">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2019/03/22/React%E5%9F%BA%E7%A1%80/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'React基础',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-06-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">React基础</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-03-21T16:00:00.000Z" title="发表于 2019-03-22 00:00:00">2019-03-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-06-02T16:00:00.000Z" title="更新于 2019-06-03 00:00:00">2019-06-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/React/">React</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">23.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>100分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="React基础"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="React基础"><a href="#React基础" class="headerlink" title="React基础"></a>React基础</h1><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>React 起源于 Facebook 的内部项目 因为该公司对市场上 JavaScript MVC框架 都不满意 就决定自己写了一套</span><br><span class="line"><span class="number">2.</span>两个概念 library(库) Framework(框架)</span><br></pre></td></tr></table></figure>
<h2 id="模块化与组件化"><a href="#模块化与组件化" class="headerlink" title="模块化与组件化"></a>模块化与组件化</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>模块化 是从代码的角度 来进行分析的(一些可复用的代码 抽离出单个的文件)</span><br><span class="line"><span class="number">2.</span>组件的 是从UI角度 来进行的分析的 (把一些 可复用的UI元素抽离成组件)</span><br></pre></td></tr></table></figure>
<h2 id="Vue-与-React-的组件"><a href="#Vue-与-React-的组件" class="headerlink" title="Vue 与 React 的组件"></a>Vue 与 React 的组件</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>VUE 创建组件 是用 创建.vue文件 来创建 主要有template script style </span><br><span class="line"><span class="number">2.</span>React  创建组件 一切都是以js来表现的(比如模板 样式 和动作)</span><br></pre></td></tr></table></figure>
<h2 id="环境的搭建"><a href="#环境的搭建" class="headerlink" title="环境的搭建"></a>环境的搭建</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>基于webpack4.x搭建  需要安装npm install webpack -D npm webpack-cli -D</span><br><span class="line"><span class="number">2.</span>我这里安装了<span class="number">5.</span>x的版本 打包文件的时候 需要执行npx webpack命令 在你没有指定webpack配置文件的时候 它会自动去找src 下的 index.js </span><br><span class="line"><span class="number">3.</span>新建文件夹 npm -init 初始化项目 然后 新建src 入口文件 src 下新建 main.js (打包的入口文件) 和 index.html 模板文件 </span><br><span class="line"><span class="number">4.</span>配置webpack-dev-server 在package.json 文件里配置脚本启动命令</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//具体版本如下 别的版本可能会有问题</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;name&quot;</span>: <span class="string">&quot;react&quot;</span>,</span><br><span class="line">  <span class="string">&quot;version&quot;</span>: <span class="string">&quot;1.0.0&quot;</span>,</span><br><span class="line">  <span class="string">&quot;description&quot;</span>: <span class="string">&quot;&quot;</span>,</span><br><span class="line">  <span class="string">&quot;main&quot;</span>: <span class="string">&quot;index.js&quot;</span>,</span><br><span class="line">  <span class="string">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;test&quot;</span>: <span class="string">&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span>,</span><br><span class="line">    <span class="string">&quot;dev&quot;</span>: <span class="string">&quot;webpack-dev-server&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;keywords&quot;</span>: [],</span><br><span class="line">  <span class="string">&quot;author&quot;</span>: <span class="string">&quot;&quot;</span>,</span><br><span class="line">  <span class="string">&quot;license&quot;</span>: <span class="string">&quot;ISC&quot;</span>,</span><br><span class="line">  <span class="string">&quot;devDependencies&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;webpack&quot;</span>: <span class="string">&quot;^4.1.1&quot;</span>,</span><br><span class="line">    <span class="string">&quot;webpack-cli&quot;</span>: <span class="string">&quot;^3.3.10&quot;</span>,</span><br><span class="line">    <span class="string">&quot;webpack-dev-server&quot;</span>: <span class="string">&quot;^3.11.0&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//配置好webpack-dev-server 之后 他会在本地开启一个静态服务访问呢  就是你输入浏览器之后就相当于 你打开你的项目文件夹 里面的文件都会如下显示 默认是访问的是根路径</span></span><br></pre></td></tr></table></figure>
<p><img src="1.png" alt=""></p>
<h2 id="webpack-dev-server"><a href="#webpack-dev-server" class="headerlink" title="webpack-dev-server"></a>webpack-dev-server</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>上面我们已经配置了webpack-dev-server的命令 并在package.json里配置 dev 脚本</span><br><span class="line"><span class="number">2.</span>用webpack-dev-server 执行的打包 会在内存中 也就是我们的项目根目录里 自动生成一个main.js 这个js是看不见的可以理解是在内存中 在项目根目录里 有这个main.js</span><br><span class="line"><span class="number">3.</span>所以我们在index.html里面直接引入即可</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>首页<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    /斜杠 就相当于项目根目录了 这里 用webpack-dev-server打包 就当于我们src下的index.js 打包到了项目根目录中main.js</span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;/main.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//配置脚本</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;name&quot;</span>: <span class="string">&quot;react&quot;</span>,</span><br><span class="line">  <span class="string">&quot;version&quot;</span>: <span class="string">&quot;1.0.0&quot;</span>,</span><br><span class="line">  <span class="string">&quot;description&quot;</span>: <span class="string">&quot;&quot;</span>,</span><br><span class="line">  <span class="string">&quot;main&quot;</span>: <span class="string">&quot;index.js&quot;</span>,</span><br><span class="line">  <span class="string">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;test&quot;</span>: <span class="string">&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span>,</span><br><span class="line">        --open 自动打开浏览器 --port 设置端口号 --host 指定ip</span><br><span class="line">    <span class="string">&quot;dev&quot;</span>: <span class="string">&quot;webpack-dev-server --open --port 3000 --hot --host 127.0.0.1&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;keywords&quot;</span>: [],</span><br><span class="line">  <span class="string">&quot;author&quot;</span>: <span class="string">&quot;&quot;</span>,</span><br><span class="line">  <span class="string">&quot;license&quot;</span>: <span class="string">&quot;ISC&quot;</span>,</span><br><span class="line">  <span class="string">&quot;devDependencies&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;webpack&quot;</span>: <span class="string">&quot;^4.1.1&quot;</span>,</span><br><span class="line">    <span class="string">&quot;webpack-cli&quot;</span>: <span class="string">&quot;^3.3.10&quot;</span>,</span><br><span class="line">    <span class="string">&quot;webpack-dev-server&quot;</span>: <span class="string">&quot;^3.11.0&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="html-webpack-plugin插件"><a href="#html-webpack-plugin插件" class="headerlink" title="html-webpack-plugin插件"></a>html-webpack-plugin插件</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>刚刚我们配置的时候把js 文件直接打包进内存中 在根目录下</span><br><span class="line"><span class="number">2.</span>我们默认访问index.html的时候 还需要点进src下 才会读出来你的index.html</span><br><span class="line"><span class="number">3.</span>所以html-webpack-plugin 插件 就是把我们的html文件也打包进根目录的内存中 这样 你已启动项目 浏览器就直接能读出index.html</span><br><span class="line"><span class="number">4.</span>也可以用来配置模板文件 只当不同的html 或者ejs文件</span><br><span class="line"><span class="number">5.</span>这个插件会自动帮我们引入打包后的js文件 所以我们在index.html模板中就不用在写了script标签了</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> htmlPlugin = <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">    template:path.join(__dirname,<span class="string">&#x27;./src/index.html&#x27;</span>), <span class="comment">//那个文件为模板</span></span><br><span class="line">    filename:<span class="string">&#x27;index.html&#x27;</span> <span class="comment">//放在内存中文件的名称</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    plugins:[</span><br><span class="line">        htmlPlugin <span class="comment">//注册插件</span></span><br><span class="line">    ]</span><br><span class="line">        </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="配置省略后缀名"><a href="#配置省略后缀名" class="headerlink" title="配置省略后缀名"></a>配置省略后缀名</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们在引入组件的时候 经常会带入.jsx  .vue .json .js后缀名</span><br><span class="line"><span class="number">2.</span>可以通过webpack 配置 去省略</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> htmlPlugin = <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">    title:<span class="string">&#x27;React学习&#x27;</span>,</span><br><span class="line">    inject:<span class="literal">true</span>,</span><br><span class="line">    template:path.join(__dirname,<span class="string">&#x27;./src/index.html&#x27;</span>),</span><br><span class="line">    filename:<span class="string">&#x27;index.html&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    plugins:[</span><br><span class="line">        htmlPlugin</span><br><span class="line">    ],</span><br><span class="line">    resolve:&#123;</span><br><span class="line">        <span class="comment">//后缀名称可以不写</span></span><br><span class="line">        extensions:[<span class="string">&#x27;.js&#x27;</span>,<span class="string">&#x27;.jsx&#x27;</span>,<span class="string">&#x27;.json&#x27;</span>]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//第三方模块的配置规则</span></span><br><span class="line">    <span class="built_in">module</span>:&#123;</span><br><span class="line">        rules:[</span><br><span class="line">            <span class="comment">//以js解为 或者 jsx 结尾的 使用 babel-loader去转换 排除 node_modules下的js文件</span></span><br><span class="line">            &#123;<span class="attr">test</span>:<span class="regexp">/\.js|jsx/</span>,use:<span class="string">&#x27;babel-loader&#x27;</span>,<span class="attr">exclude</span>:<span class="regexp">/node_modules/</span>&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">        </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="配置根目录标识符"><a href="#配置根目录标识符" class="headerlink" title="配置根目录标识符"></a>配置根目录标识符</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>可以配置一个符号 来代表 根目录的路径</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> htmlPlugin = <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">    title:<span class="string">&#x27;React学习&#x27;</span>,</span><br><span class="line">    inject:<span class="literal">true</span>,</span><br><span class="line">    template:path.join(__dirname,<span class="string">&#x27;./src/index.html&#x27;</span>),</span><br><span class="line">    filename:<span class="string">&#x27;index.html&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    plugins:[</span><br><span class="line">        htmlPlugin</span><br><span class="line">    ],</span><br><span class="line">    resolve:&#123;</span><br><span class="line">        <span class="comment">//后缀名称可以不写</span></span><br><span class="line">        extensions:[<span class="string">&#x27;.js&#x27;</span>,<span class="string">&#x27;.jsx&#x27;</span>,<span class="string">&#x27;.json&#x27;</span>],</span><br><span class="line">        alias: &#123;</span><br><span class="line">            <span class="string">&#x27;@&#x27;</span>:path.resolve(__dirname,<span class="string">&#x27;./src&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//第三方模块的配置规则</span></span><br><span class="line">    <span class="built_in">module</span>:&#123;</span><br><span class="line">        rules:[</span><br><span class="line">            <span class="comment">//以js解为 或者 jsx 结尾的 使用 babel-loader去转换 排除 node_modules下的js文件</span></span><br><span class="line">            &#123;<span class="attr">test</span>:<span class="regexp">/\.js|jsx/</span>,use:<span class="string">&#x27;babel-loader&#x27;</span>,<span class="attr">exclude</span>:<span class="regexp">/node_modules/</span>&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">        </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="安装react-react-dom"><a href="#安装react-react-dom" class="headerlink" title="安装react react-dom"></a>安装react react-dom</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>在项目中 使用react 需要安装两个包 一个react 一个是react-dom</span><br><span class="line"><span class="number">2.</span>react 专门用于创建组件和虚拟DOM 组件的生命周期都在其中</span><br><span class="line">react-dom 专门进行DOM操作的 最主要的应用场景 就是ReactDom.render() 把虚拟DOM渲染到页面上</span><br><span class="line"><span class="number">3.</span> 安装 npm install react react-dom -s</span><br></pre></td></tr></table></figure>
<h3 id="创建和渲染"><a href="#创建和渲染" class="headerlink" title="创建和渲染"></a>创建和渲染</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//引入 必须要这么写</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span> <span class="comment">//创建组件 生命周期 虚拟DOM</span></span><br><span class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">&#x27;react-dom&#x27;</span> <span class="comment">//把创建的虚拟DOM 渲染到页面上</span></span><br><span class="line"><span class="comment">//创建虚拟DOM元素 </span></span><br><span class="line"><span class="comment">//参数1 创建元素的类型 字符串 表示元素名称</span></span><br><span class="line"><span class="comment">//参数2 是一个对象或者null 表示当前这个DOM 元素的属性</span></span><br><span class="line"><span class="comment">//参数3 子节点（包括 其他 虚拟DOM 获取 文本子几点）</span></span><br><span class="line"><span class="comment">//参数n 其他子节点</span></span><br><span class="line"><span class="comment">//创建 &lt;h1 id=&quot;myh1&quot; title=&quot;this is a h1&quot;&gt;这是一个大大的H1&lt;/h1&gt;</span></span><br><span class="line"><span class="keyword">const</span> myh1 = React.createElement(<span class="string">&#x27;h1&#x27;</span>,&#123;</span><br><span class="line">    id:<span class="string">&#x27;myh1&#x27;</span>,</span><br><span class="line">    title:<span class="string">&#x27;this is a h1&#x27;</span></span><br><span class="line">&#125;,<span class="string">&#x27;这是一个大大的h1&#x27;</span>)</span><br><span class="line"><span class="comment">//使用ReactDOM.render() 把虚拟DOM渲染到页面上</span></span><br><span class="line"><span class="comment">//参数1 需要渲染的虚拟DOM</span></span><br><span class="line"><span class="comment">//参数2 要把虚拟DOM 渲染到哪个容器上</span></span><br><span class="line">ReactDOM.render(myh1,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//最终html页面上的显示</span></span><br><span class="line">&lt;h1 id=<span class="string">&quot;myh1&quot;</span> title=<span class="string">&quot;this is a h1&quot;</span>&gt;这是一个大大的h1&lt;/h1&gt;</span><br></pre></td></tr></table></figure>
<h3 id="元素嵌套"><a href="#元素嵌套" class="headerlink" title="元素嵌套"></a>元素嵌套</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> myh1 = React.createElement(<span class="string">&#x27;h1&#x27;</span>,&#123;</span><br><span class="line">    id:<span class="string">&#x27;myh1&#x27;</span>,</span><br><span class="line">    title:<span class="string">&#x27;this is a h1&#x27;</span></span><br><span class="line">&#125;,<span class="string">&#x27;这是一个大大的h1&#x27;</span>)</span><br><span class="line"><span class="comment">//把需要嵌套的虚拟DOM在放在 React.createElment最后的面的参数即可</span></span><br><span class="line"><span class="keyword">const</span> mydiv = React.createElement(<span class="string">&#x27;div&#x27;</span>,<span class="literal">null</span>,<span class="string">&#x27;我是div的子节点&#x27;</span>,myh1)</span><br></pre></td></tr></table></figure>
<h2 id="使用JSX语法"><a href="#使用JSX语法" class="headerlink" title="使用JSX语法"></a>使用JSX语法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们以上都是在js里写 虚拟DOM 现在想使用标签的形式来写虚拟DOM</span><br><span class="line"><span class="number">2.</span>所以这里就使用到了jsx  可以使用babel 来转换 需要安装对应的loader </span><br><span class="line"><span class="number">3.</span>babel-core babel-loader babel-plugin-transform-runtime -D</span><br><span class="line">babel-preset-env babel-preset-stage-<span class="number">0</span> -D</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//JSX语法 </span></span><br><span class="line"><span class="keyword">const</span> mydiv = <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;mydiv&quot;</span> <span class="attr">title</span>=<span class="string">&quot;this is div&quot;</span>&gt;</span>这是一个div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="comment">//这里通过babel 再次转换 就变成了 我们react这种创建虚拟DOM的形式</span></span><br><span class="line">React.createElement(<span class="string">&#x27;div&#x27;</span>,&#123;<span class="attr">id</span>:<span class="string">&#x27;mydiv&#x27;</span>,title=<span class="string">&quot;this is div&quot;</span>&#125;,<span class="string">&#x27;这是一个div&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h2 id="配置babel"><a href="#配置babel" class="headerlink" title="配置babel"></a>配置babel</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们需要安装 babel的插件 babel-core babel-loader babel-plugin-transform-runtime -D</span><br><span class="line">babel-preset-env babel-preset-stage-<span class="number">0</span> -D</span><br><span class="line"><span class="number">2.</span>安装能够识别转换JSX语法的 babel-preset-react 这个包负责把jsx转换 先识别然后通过我们上面安装的那些插件包再去转换</span><br><span class="line"><span class="number">3.</span>配置.babelrc 文件 主要去识别JSX语法</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//首先我们在webpack.config.js下 配置转换jsx 或者jsx语法的laoder 使用babel-loader</span></span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> htmlPlugin = <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">    title:<span class="string">&#x27;React学习&#x27;</span>,</span><br><span class="line">    inject:<span class="literal">true</span>,</span><br><span class="line">    template:path.join(__dirname,<span class="string">&#x27;./src/index.html&#x27;</span>),</span><br><span class="line">    filename:<span class="string">&#x27;index.html&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    plugins:[</span><br><span class="line">        htmlPlugin</span><br><span class="line">    ],</span><br><span class="line">    <span class="comment">//第三方模块的配置规则</span></span><br><span class="line">    <span class="built_in">module</span>:&#123;</span><br><span class="line">        rules:[</span><br><span class="line">            <span class="comment">//以js解为 或者 jsx 结尾的 使用 babel-loader去转换 排除 node_modules下的js文件</span></span><br><span class="line">            &#123;<span class="attr">test</span>:<span class="regexp">/\.js|jsx/</span>,use:<span class="string">&#x27;babel-loader&#x27;</span>,<span class="attr">exclude</span>:<span class="regexp">/node_modules/</span>&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">        </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//然后 我们要配置.babelrc 的配置文件 这个文件 主要告诉webpack 当我们用babel-loader 去转换jsx语法的时候 按照哪个babel的规则去转换</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;preset&quot;</span>:[<span class="string">&quot;env&quot;</span>,<span class="string">&quot;stage-0&quot;</span>,<span class="string">&quot;react&quot;</span>],</span><br><span class="line">    <span class="string">&quot;plugins&quot;</span>: [<span class="string">&quot;transform-runtime&quot;</span>]</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//这里需要注意以下 如果  你是先安装的react   这里装完babel 之后 最好 在把react重新装一下</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//这样就可以些jsx语法了</span></span><br><span class="line"><span class="keyword">const</span> mydiv = <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;mydiv&quot;</span> <span class="attr">title</span>=<span class="string">&quot;this is div&quot;</span>&gt;</span>这是一个div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h2 id="JSX语法"><a href="#JSX语法" class="headerlink" title="JSX语法"></a>JSX语法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>JSX语法中 只要涉及到变量的形式 渲染到jsx中语法中 都要用&#123; &#125; 大括号包裹起来</span><br></pre></td></tr></table></figure>
<h3 id="JSX基本语法"><a href="#JSX基本语法" class="headerlink" title="JSX基本语法"></a>JSX基本语法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//这里一定要用花括号包裹起来 </span></span><br><span class="line"><span class="keyword">const</span> str = <span class="number">111</span></span><br><span class="line"><span class="keyword">const</span> bl = <span class="literal">true</span></span><br><span class="line"><span class="keyword">const</span> tl = <span class="string">&#x27;我是P标签的title&#x27;</span></span><br><span class="line"><span class="keyword">const</span> h1 = <span class="xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>这是一个jsx元素<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="keyword">const</span> mydiv = <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;mydiv&quot;</span> <span class="attr">title</span>=<span class="string">&quot;this is div&quot;</span>&gt;</span></span></span><br><span class="line"><span class="xml">    &#123;10 + str&#125;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">hr</span> /&gt;</span></span></span><br><span class="line"><span class="xml">    &#123;/* 三元表达式 */&#125;</span></span><br><span class="line"><span class="xml">    &#123;bl? &#x27;我的布尔值是真&#x27;:&#x27;我的布尔值是假&#x27;&#125;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">hr</span>&gt;</span><span class="tag">&lt;/<span class="name">hr</span>&gt;</span></span></span><br><span class="line"><span class="xml">    &#123;/* 属性绑定值 */&#125;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">p</span> <span class="attr">title</span>=<span class="string">&#123;tl&#125;</span>&gt;</span>这是一个p标签<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">hr</span>&gt;</span><span class="tag">&lt;/<span class="name">hr</span>&gt;</span></span></span><br><span class="line"><span class="xml">    &#123;/* 渲染JSX元素 */&#125;</span></span><br><span class="line"><span class="xml">    &#123;h1&#125;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h3 id="渲染数组"><a href="#渲染数组" class="headerlink" title="渲染数组"></a>渲染数组</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//当数组里的每个元素 都是jsx元素的时候 直接就可以使用下面这种形式在页面上渲染 它会自己循环去渲染 但是要指定key</span></span><br><span class="line"><span class="keyword">const</span> arr = [</span><br><span class="line">    &lt;h1 key=&#123;<span class="number">0</span>&#125;&gt;我是h1&lt;/h1&gt;,</span><br><span class="line">    &lt;h2 key=&#123;<span class="number">1</span>&#125;&gt;我是h2&lt;/h2&gt;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> mydiv = <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;mydiv&quot;</span> <span class="attr">title</span>=<span class="string">&quot;this is div&quot;</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">hr</span>&gt;</span><span class="tag">&lt;/<span class="name">hr</span>&gt;</span></span></span><br><span class="line"><span class="xml">    &#123;/* 渲染数组 */&#125;</span></span><br><span class="line"><span class="xml">    &#123;arr&#125;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//当数组里的每个元素是字符串的时候 要先循环素组 然后 循环每一项 用标签包裹起来 变成 jsx元素的数组</span></span><br><span class="line"><span class="keyword">const</span> arrstr = [<span class="string">&#x27;神乐&#x27;</span>,<span class="string">&#x27;新八&#x27;</span>,<span class="string">&#x27;啊银&#x27;</span>]</span><br><span class="line"><span class="keyword">const</span> mydiv = <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;mydiv&quot;</span> <span class="attr">title</span>=<span class="string">&quot;this is div&quot;</span>&gt;</span></span></span><br><span class="line"><span class="xml">    &#123;/* 渲染字符串数组 先把数组循环遍历成jsx数组 */&#125;</span></span><br><span class="line"><span class="xml">    &#123;arrstr.map( item =&gt; <span class="tag">&lt;<span class="name">p</span> <span class="attr">key</span>=<span class="string">&#123;item&#125;</span>&gt;</span>&#123;item&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span>)&#125;</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h3 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项"></a>注意事项</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>关于注释 <span class="comment">/*  这是注释的内容  */</span></span><br><span class="line"><span class="number">2.</span>如果要给jsx元素 绑定类的话 需要用className去绑定 label的 <span class="keyword">for</span> 要用 htmlFor &lt;label htmlFor&gt;</span><br><span class="line"><span class="number">3.</span> 所有的节点 都有且只有一个根元素</span><br></pre></td></tr></table></figure>
<h2 id="React中的条件渲染"><a href="#React中的条件渲染" class="headerlink" title="React中的条件渲染"></a>React中的条件渲染</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们常常会根据 某一个数据的状态 去渲染固定的元素</span><br><span class="line"><span class="number">2.</span>这里的逻辑 我们可以写在render里  </span><br><span class="line"><span class="number">3.</span>也可以写在 我们render 函数里 <span class="keyword">return</span> 里</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//条件渲染 比如 我们有登录信息之后 请显示请登录 如果没有 请 显示显示登录按钮</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Demo</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            isLog:<span class="literal">false</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> showView = <span class="built_in">this</span>.state.isLog?<span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>lilei<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>:<span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>请登录<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            条件渲染:&#123;showView&#125; &lt;button&gt;请登录&lt;/button&gt;</span><br><span class="line">    	    &#123;<span class="comment">/*还可以直接 在return里面去写一些判断的表达式 */</span>&#125;</span><br><span class="line">		    &#123;</span><br><span class="line">                <span class="built_in">this</span>.state.isLog?</span><br><span class="line">                &lt;div&gt;我是为真的时候渲染&lt;/div&gt;</span><br><span class="line">                :</span><br><span class="line">                &lt;div&gt;我是为假的时候渲染&lt;/div&gt;</span><br><span class="line">            &#125;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>
<h2 id="受控和非受控组件-表单"><a href="#受控和非受控组件-表单" class="headerlink" title="受控和非受控组件(表单)"></a>受控和非受控组件(表单)</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>受控组件和非受控组件 主要是针对于表单元素 来说的</span><br><span class="line"><span class="number">2.</span>所谓受控组件 就是 表单 input绑定的是我们的自己的状态值</span><br><span class="line"><span class="number">3.</span>而非受控组件 就是表单 input 是通过ref之后 实时获取的</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//受控组件</span></span><br><span class="line"><span class="keyword">import</span> React, &#123; Component &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</span><br><span class="line">  state = &#123;</span><br><span class="line">    value:<span class="string">&#x27;你好&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">  hendler = <span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">      value:e.target.value</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">        &#123;<span class="built_in">this</span>.state.value&#125;</span><br><span class="line">        &#123;<span class="comment">/*这个值 是绑定的我们自己的状态 而且当改变的时候需要我们自己去设置我们自己的状态*/</span>&#125;</span><br><span class="line">        &lt;input type=<span class="string">&quot;text&quot;</span> value=&#123;<span class="built_in">this</span>.state.value&#125; onChange=&#123;<span class="built_in">this</span>.hendler&#125;/&gt;        </span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//非受控组件  就是 值 不用我们自己去设置状态 当文本框输入完成 我们在去取他的值</span></span><br><span class="line"><span class="keyword">import</span> React, &#123; Component &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">super</span>()</span><br><span class="line">    <span class="comment">//1. 通过   React.createRef() 指定一个值</span></span><br><span class="line">    <span class="built_in">this</span>.username = React.createRef()</span><br><span class="line">  &#125;</span><br><span class="line">   hendler = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//当我们输入完毕之后 点击按钮 通过 this.username.current.value就能获取到   </span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>.username.current.value)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">        &#123;<span class="comment">/*2.然后绑定在ref上*/</span>&#125;   </span><br><span class="line">        &lt;input type=<span class="string">&quot;text&quot;</span> ref=&#123;<span class="built_in">this</span>.username&#125; /&gt;    </span><br><span class="line">        &lt;button onClick=&#123;<span class="built_in">this</span>.hendler&#125;&gt;按钮&lt;/button&gt;    </span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="状态提升"><a href="#状态提升" class="headerlink" title="状态提升"></a>状态提升</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>比如我们两个input框 分别在不同的子组件里 但是 我们在同一个父组件引入了他 且命名为Child1组件 和Child2组件</span><br><span class="line"><span class="number">2.</span>当我们Child1组件中 改变 input的值的时候 Child2组件中的input值也需要改变</span><br><span class="line"><span class="number">3.</span>这里我们会在父组件定义一个值 然后分别传给 我们的 Child1 和 Child2 </span><br><span class="line"><span class="number">4.</span>当我们 Child1组件改变的时候  就通过传给Child1的回调函数 把我们改变之后的传过去 然后在赋值给我们的父组件本身的值</span><br><span class="line"><span class="number">5.</span>然后 Child2在 getDerivedStateFromProps 生命周期中 重新改变状态</span><br></pre></td></tr></table></figure>
<p><img src="8.gif" alt=""></p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//父组件</span></span><br><span class="line"><span class="keyword">import</span> React, &#123; Component &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Child1 <span class="keyword">from</span> <span class="string">&#x27;./Child1&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Child2 <span class="keyword">from</span> <span class="string">&#x27;./Child2&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">super</span>()</span><br><span class="line">   <span class="number">1.</span>定义 值传给 两个子组件   </span><br><span class="line">   <span class="built_in">this</span>.state = &#123;</span><br><span class="line">     menoy:<span class="number">1</span></span><br><span class="line">   &#125;</span><br><span class="line">  &#125; </span><br><span class="line">  <span class="number">2.</span>当child1组件 输入框改变的时候 把改变后的值传送回来 在赋值给我们的父组件  </span><br><span class="line">  update = <span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(res)</span><br><span class="line">    <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">      menoy:res</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">       &lt;p&gt;父组件&lt;/p&gt;</span><br><span class="line">       &lt;div&gt;</span><br><span class="line">         人民币 &lt;Child1 menoy=&#123;<span class="built_in">this</span>.state.menoy&#125; update=&#123;<span class="built_in">this</span>.update&#125;/&gt;</span><br><span class="line">       &lt;/div&gt;</span><br><span class="line">       &lt;div&gt;</span><br><span class="line">         美元 &lt;Child2 menoy=&#123;<span class="built_in">this</span>.state.menoy&#125; /&gt;</span><br><span class="line">       &lt;/div&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Child1组件</span></span><br><span class="line"><span class="keyword">import</span> React, &#123; Component &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Child1</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            input1:<span class="number">1</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//初始的时候 就把值设置称我们父组件传过来的</span></span><br><span class="line">    <span class="function"><span class="title">componentDidMount</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            input1:<span class="built_in">this</span>.props.menoy</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//改变的时候 把input的框设置称我们改变的值 然后在调用父组件传过来的函数 把值传送回去</span></span><br><span class="line">    handler = <span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            input1:e.target.value</span><br><span class="line">        &#125;,<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">this</span>.props.update(<span class="built_in">this</span>.state.input1)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;div&gt;</span><br><span class="line">                &lt;input type=<span class="string">&quot;text&quot;</span> value=&#123;<span class="built_in">this</span>.state.input1&#125; onChange=&#123;<span class="built_in">this</span>.handler&#125;/&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React, &#123; Component &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="comment">//Child2组件</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Child2</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            input2:<span class="number">1</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//最开始的时候 把 值换算称 我们美元的值</span></span><br><span class="line">    <span class="function"><span class="title">componentDidMount</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            input2:<span class="built_in">this</span>.props.menoy * <span class="number">7</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//因为 你Child1 组件 每次输入的文本都会改变 所以我们要在这个生命周期中监测一下 父组件实时传过来的值</span></span><br><span class="line">    <span class="keyword">static</span> <span class="function"><span class="title">getDerivedStateFromProps</span>(<span class="params">prv,curr</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(prv.menoy !== curr.input2)&#123;</span><br><span class="line">            <span class="keyword">return</span> &#123;</span><br><span class="line">                ...curr,</span><br><span class="line">                input2:prv.menoy * <span class="number">7</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">    handler = <span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            input2:<span class="built_in">this</span>.props.menoy * <span class="number">7</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;div&gt;</span><br><span class="line">                &lt;input type=<span class="string">&quot;text&quot;</span> value=&#123;<span class="built_in">this</span>.state.input2&#125; onChange=&#123;<span class="built_in">this</span>.handler&#125;/&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="React创建组件"><a href="#React创建组件" class="headerlink" title="React创建组件"></a>React创建组件</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>React 一共有两种创建组件的方式</span><br><span class="line"><span class="number">2.</span>一种是函数时组件(没有自己的状态) 一种是类组件(有自己的状态)</span><br></pre></td></tr></table></figure>
<h3 id="函数式组件"><a href="#函数式组件" class="headerlink" title="函数式组件"></a>函数式组件</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>一个构造函数 就是一个函数式的组件</span><br><span class="line"><span class="number">2.</span>构造函数里 必须要 <span class="keyword">return</span> 一个虚拟DOM</span><br><span class="line"><span class="number">3.</span>如果<span class="keyword">return</span> 一个 <span class="literal">null</span> 就是什么都不渲染</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Hellow</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>我是Hellow组件<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    我是div</span><br><span class="line">    &lt;Hellow&gt;&lt;/Hellow&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h3 id="函数式组件的传值与接收"><a href="#函数式组件的传值与接收" class="headerlink" title="函数式组件的传值与接收"></a>函数式组件的传值与接收</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>接收的时候是在函数式组件里的props形参中接收的</span><br><span class="line"><span class="number">2.</span>传值的时候还是要自己指定key 然后 值式你要传入的值</span><br><span class="line"><span class="number">3.</span>子组件跟vue一样 不允许 改变 传过来的值</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//这里是要传入的值</span></span><br><span class="line"><span class="keyword">const</span> dog = &#123;</span><br><span class="line">    name:<span class="string">&#x27;大黄&#x27;</span>,</span><br><span class="line">    age:<span class="number">19</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//函数式组件 用props 去接收</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Hellow</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(props)</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>我是Hellow组件 &#123;props.name&#125; <span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    我是div</span><br><span class="line">    &lt;Hellow name=&#123;dog.name&#125; age=&#123;dog.age&#125;&gt;&lt;/Hellow&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//展开写法</span></span><br><span class="line"><span class="keyword">const</span> dog = &#123;</span><br><span class="line">    name:<span class="string">&#x27;大黄&#x27;</span>,</span><br><span class="line">    age:<span class="number">19</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Hellow</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(props)</span><br><span class="line"><span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>我是Hellow组件 &#123;props.name&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    我是div</span><br><span class="line">     <span class="comment">/* 把值展开 即可*/</span></span><br><span class="line">    &lt;Hellow &#123;...dog&#125;&gt;&lt;/Hellow&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<p><img src="2.png" alt=""></p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//抽离出来的jsx组件</span></span><br><span class="line"><span class="comment">//单独 新建一个component文件夹 下面新建一个hellow.jsx文件 </span></span><br><span class="line"><span class="comment">//这里一定要再次引入react 因为你下面些的jsx语法 是要转换成React.createElment()</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span> </span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">Hellow</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(props)</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>我是抽离出来的Hellow组件<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//在index.js种引入</span></span><br><span class="line"><span class="keyword">const</span> dog = &#123;</span><br><span class="line">    name:<span class="string">&#x27;大黄&#x27;</span>,</span><br><span class="line">    age:<span class="number">19</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">import</span> Hellow <span class="keyword">from</span> <span class="string">&#x27;./component/hellow.jsx&#x27;</span></span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    我是div</span><br><span class="line">    &lt;Hellow &#123;...dog&#125;&gt;&lt;/Hellow&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h3 id="函数是组件默认值的设置"><a href="#函数是组件默认值的设置" class="headerlink" title="函数是组件默认值的设置"></a>函数是组件默认值的设置</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里我们需要使用静态成员的方式去设置</span><br><span class="line"><span class="number">2.</span>函数名.defaultProps =&#123;&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">UserLog</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line"><span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>欢迎登录 &#123;props.name&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//这里需要用静态成员的属性去设置默认值</span></span><br><span class="line">UserLog.defaultProps =&#123;</span><br><span class="line">    name:<span class="string">&#x27;我是默认值&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">UserNoLog</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>请登录<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="Class组件-类组件"><a href="#Class组件-类组件" class="headerlink" title="Class组件(类组件)"></a>Class组件(类组件)</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>Class组件 要继承 React.Component</span><br><span class="line"><span class="number">2.</span>用Class关键字 创建出来的组件 是有自己的状态的 有state </span><br><span class="line"><span class="number">3.</span>在<span class="class"><span class="keyword">class</span>组件里 需要有一个<span class="title">render</span>函数 <span class="title">return</span> <span class="title">JSX</span>元素</span></span><br><span class="line"><span class="class">4.<span class="title">React</span>中的父子传入数据 属于单项数据流 就是子组件不能更改父组件传入的数据 所有数据只能在父组件中改 然后在在重新传入到子组件中</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用class </span></span><br><span class="line"><span class="keyword">import</span> React,&#123;Component&#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span> </span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Hellow</span> <span class="keyword">extends</span> <span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>我是class关键字搞出来的组件<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="class组件传参"><a href="#class组件传参" class="headerlink" title="class组件传参"></a>class组件传参</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用class创建的组件 传进来的参数 不需要接收 直接使用this.props.就可以接收</span></span><br><span class="line"><span class="keyword">const</span> dog = &#123;</span><br><span class="line">    name:<span class="string">&#x27;大黄&#x27;</span>,</span><br><span class="line">    age:<span class="number">19</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">import</span> Hellow <span class="keyword">from</span> <span class="string">&#x27;@/component/hellow&#x27;</span></span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    我是div</span><br><span class="line">	<span class="comment">//class 创建出来的组件 传参</span></span><br><span class="line">    &lt;Hellow &#123;...dog&#125;&gt;&lt;/Hellow&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br><span class="line"><span class="comment">//接收 直接用 this.props.来接收</span></span><br><span class="line"><span class="keyword">import</span> React,&#123;Component&#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span> </span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Hellow</span> <span class="keyword">extends</span> <span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>我是class关键字搞出来的组件 &#123;this.props.name&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="class组件中的this-state"><a href="#class组件中的this-state" class="headerlink" title="class组件中的this.state"></a>class组件中的this.state</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>类组件都是有自己的状态 </span><br><span class="line"><span class="number">2.</span>自己的状态就state 是个对象 需要定义在自己的构造器里 每个类 都要继承React.component </span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Ctm</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            ctmList : [</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">1</span>,<span class="attr">user</span>:<span class="string">&#x27;盖伦&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;人在塔在&#x27;</span>&#125;,</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">2</span>,<span class="attr">user</span>:<span class="string">&#x27;拉克丝&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;命运与你同在&#x27;</span>&#125;,</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">3</span>,<span class="attr">user</span>:<span class="string">&#x27;哪吒&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;抽你筋,扒你的皮&#x27;</span>&#125;,</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">4</span>,<span class="attr">user</span>:<span class="string">&#x27;花木兰&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;离家太远会忘记故乡&#x27;</span>&#125;</span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> &lt;div&gt;</span><br><span class="line">            &lt;h1&gt;我是评论列表&lt;/h1&gt;</span><br><span class="line">            &#123;<span class="built_in">this</span>.state.ctmList.map( <span class="function"><span class="params">item</span> =&gt;</span> <span class="xml"><span class="tag">&lt;<span class="name">CmItem</span> &#123;<span class="attr">...item</span>&#125; <span class="attr">key</span>=<span class="string">&#123;item.id&#125;/</span>&gt;</span></span>)&#125;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(<span class="xml"><span class="tag">&lt;<span class="name">Ctm</span>/&gt;</span></span>,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h3 id="使用this-setState修改state数据"><a href="#使用this-setState修改state数据" class="headerlink" title="使用this.setState修改state数据"></a>使用this.setState修改state数据</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>React 中不像Vue中 你数据变了视图自己会变 如果要给state中的数据重新赋值 就要用setState</span><br><span class="line"><span class="number">2.</span>setState函数中 传入一个对象 设置你要修改的state中的值</span><br><span class="line"><span class="number">3.</span>setState这个方法 是异步的 所以有了第二个参数 是个回调可以写你同步的代码</span><br><span class="line"><span class="number">4.</span><span class="built_in">this</span>.setState 会引起视图的重绘 在可控的情况下 是异步的 在非可控的情况下 是同步的</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyEvent</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">        <span class="built_in">this</span>.state =&#123;</span><br><span class="line">            msg:<span class="string">&#x27;我是state中的数据&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> &#123;this.say(&#x27;我是传入的参数&#x27;)&#125;&#125;&gt;&#123;this.state.msg&#125;<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">say</span>(<span class="params">str</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//传入一个对象 传入你要修改的state中的值</span></span><br><span class="line">       <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">           msg:<span class="string">&#x27;我被改变了&#x27;</span></span><br><span class="line">       &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    &lt;MyEvent/&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//setState方法是异步的 要想写入同步的代码 就要在这个回调里面去写</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyEvent</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">        <span class="built_in">this</span>.state =&#123;</span><br><span class="line">            msg:<span class="string">&#x27;我是state中的数据&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> &#123;this.say(&#x27;我是传入的参数&#x27;)&#125;&#125;&gt;&#123;this.state.msg&#125;<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">say</span>(<span class="params">str</span>)</span>&#123;</span><br><span class="line">       <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">           msg:<span class="string">&#x27;我被改变了&#x27;</span></span><br><span class="line">       &#125;,<span class="function">() =&gt;</span> &#123;</span><br><span class="line">           <span class="built_in">console</span>.log(<span class="built_in">this</span>.state.msg)</span><br><span class="line">       &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    &lt;MyEvent/&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h3 id="子组件向父组件传递数据"><a href="#子组件向父组件传递数据" class="headerlink" title="子组件向父组件传递数据"></a>子组件向父组件传递数据</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>React 中没有向Vue中那样 可以使用$emit去触发事件函数 在回调里 拿到数据的值</span><br><span class="line"><span class="number">2.</span>React 中 子向父 传值 是通过 父组件 向 子组件中传递一个函数完成</span><br><span class="line"><span class="number">3.</span>注意 这里 要传递的函数 一定是一个箭头函数</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ChildView</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            cmsg:<span class="string">&#x27;我是子组件的数据&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.props)</span><br><span class="line">        <span class="comment">//2.在点击的时候 我们通过props直接调用这个函数即可 也是采用箭头函数的形式</span></span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;button onClick=&#123;<span class="function">() =&gt;</span> &#123;<span class="built_in">this</span>.props.setpmsg(<span class="built_in">this</span>.state.cmsg)&#125;&#125;&gt;我是子组件中的按钮 我要向父组件发送数据&lt;/button&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//1.首先在父组件里 向子组件里传递一个函数 注意 这个要传递的函数一定是箭头函数的写法 否则 this会有问题</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ParentView</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">        <span class="built_in">this</span>.state =&#123;</span><br><span class="line">            pmsg:<span class="string">&#x27;我是父组件的数据&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//如果我们下面的方法 不用箭头函数的化 就必须要用这个bind 去改变this的指向</span></span><br><span class="line">        <span class="built_in">this</span>.updata = <span class="built_in">this</span>.updata.bind(<span class="built_in">this</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;div&gt;</span><br><span class="line">                &lt;div&gt;我是父组件&lt;/div&gt;</span><br><span class="line">                &lt;div&gt;&#123;<span class="built_in">this</span>.state.pmsg&#125;&lt;/div&gt;</span><br><span class="line">                &lt;ChildView setpmsg=&#123;<span class="built_in">this</span>.updata&#125;&gt;&lt;/ChildView&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">    updata=<span class="function">(<span class="params">data</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            pmsg:data</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">ReactDOM.render(<span class="xml"><span class="tag">&lt;<span class="name">ParentView</span>/&gt;</span></span>,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h2 id="组件的生命周期"><a href="#组件的生命周期" class="headerlink" title="组件的生命周期"></a>组件的生命周期</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 挂载卸载过程</span><br><span class="line"><span class="number">1.1</span>.constructor() 初始构造器 </span><br><span class="line"><span class="number">1.2</span>.componentWillMount() 将要把虚拟DOM挂载到页面上</span><br><span class="line"><span class="number">1.3</span>.componentDidMount()  已经挂载到了</span><br><span class="line"><span class="number">1.4</span>.componentWillUnmount () 卸载DOM</span><br><span class="line"><span class="number">2.</span> 更新过程</span><br><span class="line"><span class="number">2.1</span>. componentWillReceiveProps (nextProps)</span><br><span class="line"><span class="number">2.2</span>.shouldComponentUpdate(nextProps,nextState)</span><br><span class="line"><span class="number">2.3</span>.componentWillUpdate (nextProps,nextState)</span><br><span class="line"><span class="number">2.4</span>.componentDidUpdate(prevProps,prevState)</span><br><span class="line"><span class="number">2.5</span>.render()</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> React新增的生命周期(个人补充)</span><br><span class="line"><span class="number">3.1</span>. getDerivedStateFromProps(nextProps, prevState)</span><br><span class="line"><span class="number">3.2</span>. getSnapshotBeforeUpdate(prevProps, prevState)</span><br></pre></td></tr></table></figure>
<h3 id="挂载卸载过程"><a href="#挂载卸载过程" class="headerlink" title="挂载卸载过程"></a>挂载卸载过程</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.1</span>.constructor()</span><br><span class="line"><span class="title">constructor</span>(<span class="params"></span>)中完成了<span class="title">React</span>数据的初始化，它接受两个参数：<span class="title">props</span>和<span class="title">context</span>，当想在函数内部使用这两个参数时，需使用<span class="title">super</span>(<span class="params"></span>)传入这两个参数。</span><br><span class="line">注意：只要使用了<span class="title">constructor</span>(<span class="params"></span>)就必须写<span class="title">super</span>(<span class="params"></span>),否则会导致<span class="title">this</span>指向错误。</span><br><span class="line"></span><br><span class="line">1.2.<span class="title">componentWillMount</span>(<span class="params"></span>)</span><br><span class="line"><span class="title">componentWillMount</span>(<span class="params"></span>)一般用的比较少，它更多的是在服务端渲染时使用。它代表的过程是组件已经经历了<span class="title">constructor</span>(<span class="params"></span>)初始化数据后，但是还未渲染<span class="title">DOM</span>时。</span><br><span class="line"></span><br><span class="line">1.3.<span class="title">componentDidMount</span>(<span class="params"></span>)</span><br><span class="line">组件第一次渲染完成，此时<span class="title">dom</span>节点已经生成，可以在这里调用<span class="title">ajax</span>请求，返回数据<span class="title">setState</span>后组件会重新渲染</span><br><span class="line"></span><br><span class="line">1.4.<span class="title">componentWillUnmount</span> (<span class="params"></span>)</span><br><span class="line">在此处完成组件的卸载和数据的销毁。</span><br><span class="line"></span><br><span class="line"><span class="title">clear</span>你在组建中所有的<span class="title">setTimeout</span>,<span class="title">setInterval</span></span><br><span class="line">移除所有组建中的监听 <span class="title">removeEventListener</span></span><br><span class="line">有时候我们会碰到这个<span class="title">warning</span>:</span><br><span class="line"><span class="title">Can</span> <span class="title">only</span> <span class="title">update</span> <span class="title">a</span> <span class="title">mounted</span> <span class="title">or</span> <span class="title">mounting</span> <span class="title">component</span>. <span class="title">This</span> <span class="title">usually</span>      <span class="title">means</span> <span class="title">you</span> <span class="title">called</span> <span class="title">setState</span>(<span class="params"></span>) <span class="title">on</span> <span class="title">an</span> <span class="title">unmounted</span> <span class="title">component</span>. <span class="title">This</span> <span class="title">is</span> <span class="title">a</span>   <span class="title">no</span>-<span class="title">op</span>. <span class="title">Please</span> <span class="title">check</span> <span class="title">the</span> <span class="title">code</span> <span class="title">for</span> <span class="title">the</span> <span class="title">undefined</span> <span class="title">component</span>.</span><br><span class="line">原因：因为你在组件中的<span class="title">ajax</span>请求返回<span class="title">setState</span>,而你组件销毁的时候，请求还未完成，因此会报<span class="title">warning</span></span><br><span class="line">解决方法：</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="title">componentDidMount</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="built_in">this</span>.isMount === <span class="literal">true</span></span><br><span class="line">    axios.post().then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.isMount &amp;&amp; <span class="built_in">this</span>.setState(&#123;   <span class="comment">// 增加条件ismount为true时</span></span><br><span class="line">      aaa:res</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="title">componentWillUnmount</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.isMount === <span class="literal">false</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="更新过程"><a href="#更新过程" class="headerlink" title="更新过程"></a>更新过程</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">2.1</span>. componentWillReceiveProps (nextProps)</span><br><span class="line">在接受父组件改变后的props需要重新渲染组件时用到的比较多</span><br><span class="line">接受一个参数nextProps</span><br><span class="line">通过对比nextProps和<span class="built_in">this</span>.props，将nextProps的state为当前组件的state，从而重新渲染组件</span><br><span class="line">  componentWillReceiveProps (nextProps) &#123;</span><br><span class="line">    nextProps.openNotice !== <span class="built_in">this</span>.props.openNotice&amp;&amp;<span class="built_in">this</span>.setState(&#123;</span><br><span class="line">        openNotice:nextProps.openNotice</span><br><span class="line">    &#125;，() =&gt; &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="built_in">this</span>.state.openNotice:nextProps)</span><br><span class="line">      <span class="comment">//将state更新为nextProps,在setState的第二个参数（回调）可以打印出新的state</span></span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">2.2</span>.shouldComponentUpdate(nextProps,nextState)</span><br><span class="line">主要用于性能优化(部分更新)</span><br><span class="line">唯一用于控制组件重新渲染的生命周期，由于在react中，setState以后，state发生变化，组件会进入重新渲染的流程，在这里<span class="keyword">return</span> <span class="literal">false</span>可以阻止组件的更新</span><br><span class="line">因为react父组件的重新渲染会导致其所有子组件的重新渲染，这个时候其实我们是不需要所有子组件都跟着重新渲染的，因此需要在子组件的该生命周期中做判断</span><br><span class="line"></span><br><span class="line"><span class="number">2.3</span>.componentWillUpdate (nextProps,nextState)</span><br><span class="line">shouldComponentUpdate返回<span class="literal">true</span>以后，组件进入重新渲染的流程，进入componentWillUpdate,这里同样可以拿到nextProps和nextState。</span><br><span class="line"></span><br><span class="line"><span class="number">2.4</span>.componentDidUpdate(prevProps,prevState)</span><br><span class="line">组件更新完毕后，react只会在第一次初始化成功会进入componentDidmount,之后每次重新渲染后都会进入这个生命周期，这里可以拿到prevProps和prevState，即更新前的props和state。</span><br><span class="line"></span><br><span class="line"><span class="number">2.5</span>.render()</span><br><span class="line">render函数会插入jsx生成的dom结构，react会生成一份虚拟dom树，在每一次组件更新时，在此react会通过其diff算法比较更新前后的新旧DOM树，比较以后，找到最小的有差异的DOM节点，并重新渲染。</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="新增的生命周期"><a href="#新增的生命周期" class="headerlink" title="新增的生命周期"></a>新增的生命周期</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">3.1</span>. getDerivedStateFromProps(nextProps, prevState)</span><br><span class="line">代替componentWillReceiveProps()。</span><br><span class="line">老版本中的componentWillReceiveProps()方法判断前后两个 props 是否相同，如果不同再将新的 props 更新到相应的 state 上去。这样做一来会破坏 state 数据的单一数据源，导致组件状态变得不可预测，另一方面也会增加组件的重绘次数。</span><br><span class="line">举个例子:</span><br><span class="line"></span><br><span class="line"><span class="comment">// before</span></span><br><span class="line"><span class="function"><span class="title">componentWillReceiveProps</span>(<span class="params">nextProps</span>)</span> &#123;</span><br><span class="line">  <span class="keyword">if</span> (nextProps.isLogin !== <span class="built_in">this</span>.props.isLogin) &#123;</span><br><span class="line">    <span class="built_in">this</span>.setState(&#123; </span><br><span class="line">      isLogin: nextProps.isLogin,   </span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> (nextProps.isLogin) &#123;</span><br><span class="line">    <span class="built_in">this</span>.handleClose();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// after</span></span><br><span class="line"><span class="keyword">static</span> <span class="function"><span class="title">getDerivedStateFromProps</span>(<span class="params">nextProps, prevState</span>)</span> &#123;</span><br><span class="line">  <span class="keyword">if</span> (nextProps.isLogin !== prevState.isLogin) &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      isLogin: nextProps.isLogin,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">componentDidUpdate</span>(<span class="params">prevProps, prevState</span>)</span> &#123;</span><br><span class="line">  <span class="keyword">if</span> (!prevState.isLogin &amp;&amp; <span class="built_in">this</span>.props.isLogin) &#123;</span><br><span class="line">    <span class="built_in">this</span>.handleClose();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">这两者最大的不同就是:</span><br><span class="line">在 componentWillReceiveProps 中，我们一般会做以下两件事，一是根据 props 来更新 state，二是触发一些回调，如动画或页面跳转等。</span><br><span class="line"></span><br><span class="line">在老版本的 React 中，这两件事我们都需要在 componentWillReceiveProps 中去做。</span><br><span class="line">而在新版本中，官方将更新 state 与触发回调重新分配到了 getDerivedStateFromProps 与 componentDidUpdate 中，使得组件整体的更新逻辑更为清晰。而且在 getDerivedStateFromProps 中还禁止了组件去访问 <span class="built_in">this</span>.props，强制让开发者去比较 nextProps 与 prevState 中的值，以确保当开发者用到 getDerivedStateFromProps 这个生命周期函数时，就是在根据当前的 props 来更新组件的 state，而不是去做其他一些让组件自身状态变得更加不可预测的事情。</span><br><span class="line"><span class="number">3.2</span>. getSnapshotBeforeUpdate(prevProps, prevState)</span><br><span class="line">代替componentWillUpdate。</span><br><span class="line">常见的 componentWillUpdate 的用例是在组件更新前，读取当前某个 DOM 元素的状态，并在 componentDidUpdate 中进行相应的处理。</span><br><span class="line">这两者的区别在于：</span><br><span class="line"></span><br><span class="line">在 React 开启异步渲染模式后，在 render 阶段读取到的 DOM 元素状态并不总是和 commit 阶段相同，这就导致在</span><br><span class="line">componentDidUpdate 中使用 componentWillUpdate 中读取到的 DOM 元素状态是不安全的，因为这时的值很有可能已经失效了。</span><br><span class="line">getSnapshotBeforeUpdate 会在最终的 render 之前被调用，也就是说在 getSnapshotBeforeUpdate 中读取到的 DOM 元素状态是可以保证与 componentDidUpdate 中一致的。</span><br><span class="line">此生命周期返回的任何值都将作为参数传递给componentDidUpdate（）。</span><br></pre></td></tr></table></figure>
<p><img src="16775500-8d325f8093591c76.webp" alt=""></p>
<p><img src="16775500-102dbe772034e8fa.webp" alt=""></p>
<h2 id="React中的style编写"><a href="#React中的style编写" class="headerlink" title="React中的style编写"></a>React中的style编写</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>在React中要是写行内样式 必须要写花括号</span><br><span class="line"><span class="number">2.</span>并且花括号里面 也是一个对象的形式</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Ctm</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            ctmList : [</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">1</span>,<span class="attr">user</span>:<span class="string">&#x27;盖伦&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;人在塔在&#x27;</span>&#125;,</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">2</span>,<span class="attr">user</span>:<span class="string">&#x27;拉克丝&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;命运与你同在&#x27;</span>&#125;,</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">3</span>,<span class="attr">user</span>:<span class="string">&#x27;哪吒&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;抽你筋,扒你的皮&#x27;</span>&#125;,</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">4</span>,<span class="attr">user</span>:<span class="string">&#x27;花木兰&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;离家太远会忘记故乡&#x27;</span>&#125;</span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> &lt;div&gt;</span><br><span class="line">            &#123;<span class="comment">/*要用这种形式去写自定义属性 如果由-连接的 被链接的首字母要大写*/</span>&#125;</span><br><span class="line">            &lt;h1 style=&#123;&#123;<span class="attr">color</span>:<span class="string">&#x27;red&#x27;</span>&#125;&#125;&gt;我是评论列表&lt;/h1&gt;</span><br><span class="line">            &#123;<span class="built_in">this</span>.state.ctmList.map( <span class="function"><span class="params">item</span> =&gt;</span> <span class="xml"><span class="tag">&lt;<span class="name">CmItem</span> &#123;<span class="attr">...item</span>&#125; <span class="attr">key</span>=<span class="string">&#123;item.id&#125;/</span>&gt;</span></span>)&#125;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(<span class="xml"><span class="tag">&lt;<span class="name">Ctm</span>/&gt;</span></span>,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//也可以采用在外面 命名一个对象 在来写</span></span><br><span class="line"><span class="comment">//在外面 命名一个 样式的对象</span></span><br><span class="line"><span class="keyword">const</span> fontcolor = &#123;</span><br><span class="line">    color:<span class="string">&#x27;blue&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Ctm</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            ctmList : [</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">1</span>,<span class="attr">user</span>:<span class="string">&#x27;盖伦&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;人在塔在&#x27;</span>&#125;,</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">2</span>,<span class="attr">user</span>:<span class="string">&#x27;拉克丝&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;命运与你同在&#x27;</span>&#125;,</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">3</span>,<span class="attr">user</span>:<span class="string">&#x27;哪吒&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;抽你筋,扒你的皮&#x27;</span>&#125;,</span><br><span class="line">                &#123;<span class="attr">id</span>:<span class="number">4</span>,<span class="attr">user</span>:<span class="string">&#x27;花木兰&#x27;</span>,<span class="attr">content</span>:<span class="string">&#x27;离家太远会忘记故乡&#x27;</span>&#125;</span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> &lt;div&gt;</span><br><span class="line">            &#123;<span class="comment">/* 然后 在里面指定这个对象就好*/</span>&#125;</span><br><span class="line">            &lt;h1 style=&#123;fontcolor&#125;&gt;我是评论列表&lt;/h1&gt;</span><br><span class="line">            &#123;<span class="built_in">this</span>.state.ctmList.map( <span class="function"><span class="params">item</span> =&gt;</span> <span class="xml"><span class="tag">&lt;<span class="name">CmItem</span> &#123;<span class="attr">...item</span>&#125; <span class="attr">key</span>=<span class="string">&#123;item.id&#125;/</span>&gt;</span></span>)&#125;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(<span class="xml"><span class="tag">&lt;<span class="name">Ctm</span>/&gt;</span></span>,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//让然我们也可以把样式写在一个集合里 然后去使用它</span></span><br><span class="line"><span class="keyword">const</span> style = &#123;</span><br><span class="line">    item :&#123;<span class="attr">border</span>:<span class="string">&#x27;1px dashed #ccc&#x27;</span>,<span class="attr">margin</span>:<span class="string">&#x27;10px&#x27;</span>,<span class="attr">padding</span>:<span class="string">&#x27;10px&#x27;</span>,<span class="attr">boxShow</span>:<span class="string">&#x27;0 0 10px #ccc&#x27;</span>&#125;,</span><br><span class="line">    user:&#123; <span class="attr">fontSize</span>:<span class="string">&#x27;14px&#x27;</span>&#125;,</span><br><span class="line">    content:&#123;<span class="attr">fontSize</span>:<span class="string">&#x27;12px&#x27;</span>&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">CmItem</span> (<span class="params">props</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&#123;style.item&#125;</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">h1</span> <span class="attr">style</span>=<span class="string">&#123;style.user&#125;</span>&gt;</span>评论人:&#123;props.user&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">p</span> <span class="attr">style</span>=<span class="string">&#123;style.content&#125;</span>&gt;</span>评论内容:&#123;props.content&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="React使用CSS"><a href="#React使用CSS" class="headerlink" title="React使用CSS"></a>React使用CSS</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>webpack打包的时候 不认识css文件 所以我们需要安装对应的loader style-loader css-loader -D</span><br><span class="line"><span class="number">2.</span>然后在webpack中配置</span><br><span class="line"><span class="number">3.</span>然后在对应的组件中 引入我们写好的css文件 </span><br><span class="line"><span class="number">4.</span>然后直接 用className 写入你写好的css名称即可</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//webpack 配置 css loader</span></span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> htmlPlugin = <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">    title:<span class="string">&#x27;React学习&#x27;</span>,</span><br><span class="line">    inject:<span class="literal">true</span>,</span><br><span class="line">    template:path.join(__dirname,<span class="string">&#x27;./src/index.html&#x27;</span>),</span><br><span class="line">    filename:<span class="string">&#x27;index.html&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    plugins:[</span><br><span class="line">        htmlPlugin</span><br><span class="line">    ],</span><br><span class="line">    resolve:&#123;</span><br><span class="line">        <span class="comment">//后缀名称可以不写</span></span><br><span class="line">        extensions:[<span class="string">&#x27;.js&#x27;</span>,<span class="string">&#x27;.jsx&#x27;</span>,<span class="string">&#x27;.json&#x27;</span>],</span><br><span class="line">        alias: &#123;</span><br><span class="line">            <span class="string">&#x27;@&#x27;</span>:path.resolve(__dirname,<span class="string">&#x27;./src&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//第三方模块的配置规则</span></span><br><span class="line">    <span class="built_in">module</span>:&#123;</span><br><span class="line">        rules:[</span><br><span class="line">            <span class="comment">//以js解为 或者 jsx 结尾的 使用 babel-loader去转换 排除 node_modules下的js文件</span></span><br><span class="line">            &#123;<span class="attr">test</span>:<span class="regexp">/\.js|jsx/</span>,use:<span class="string">&#x27;babel-loader&#x27;</span>,<span class="attr">exclude</span>:<span class="regexp">/node_modules/</span>&#125;,</span><br><span class="line">            <span class="comment">//这里loader的加载顺序 一定式重右向左</span></span><br><span class="line">            &#123;<span class="attr">test</span>:<span class="regexp">/\.css/</span>,use:[<span class="string">&#x27;style-loader&#x27;</span>,<span class="string">&#x27;css-loader&#x27;</span>]&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">        </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//在相关的组件引入你写好的css文件 直接 使用className 指定你写好的样式名称即可</span></span><br><span class="line"><span class="comment">//引入了我们写好的css文件</span></span><br><span class="line"><span class="keyword">import</span> cssObj <span class="keyword">from</span> <span class="string">&#x27;@/css/cmitem.css&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">CmItem</span> (<span class="params">props</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//然后直接指定我们写好的css名称即可</span></span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;item&quot;</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">h1</span> <span class="attr">className</span>=<span class="string">&quot;user&quot;</span>&gt;</span>评论人:&#123;props.user&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">p</span> <span class="attr">className</span>=<span class="string">&quot;content&quot;</span>&gt;</span>评论内容:&#123;props.content&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="React中的css-配置作用域"><a href="#React中的css-配置作用域" class="headerlink" title="React中的css 配置作用域"></a>React中的css 配置作用域</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>因为我们css 没有导出 的语法 所以 导入的时候就是一个空对象  这个里面的所有 样式 都会对组件应用生效</span><br><span class="line"><span class="number">2.</span>比如 我们在写一个h1标签 的样式  如果你定义了别的h1 就都会 生效</span><br><span class="line"><span class="number">3.</span>可以用通过配置webpack 的css-loader的参数 给我们的css文件 加模块作用域</span><br><span class="line"><span class="number">4.</span>通过css-loader?modules 这样就给我们的css加上了作用域</span><br><span class="line"><span class="number">5.</span>配置好了之后 我们的导入的css文件 就增加了作用域 也就是css文件是导入了一个对象</span><br><span class="line"><span class="number">6.</span>这里的css模块话 指针对于类名 和 ID 选择器 去生效 如果 标签的话 是不会生效的</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> htmlPlugin = <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">    title:<span class="string">&#x27;React学习&#x27;</span>,</span><br><span class="line">    inject:<span class="literal">true</span>,</span><br><span class="line">    template:path.join(__dirname,<span class="string">&#x27;./src/index.html&#x27;</span>),</span><br><span class="line">    filename:<span class="string">&#x27;index.html&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    plugins:[</span><br><span class="line">        htmlPlugin</span><br><span class="line">    ],</span><br><span class="line">    resolve:&#123;</span><br><span class="line">        extensions:[<span class="string">&#x27;.js&#x27;</span>,<span class="string">&#x27;.jsx&#x27;</span>,<span class="string">&#x27;.json&#x27;</span>],</span><br><span class="line">        alias: &#123;</span><br><span class="line">            <span class="string">&#x27;@&#x27;</span>:path.resolve(__dirname,<span class="string">&#x27;./src&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="built_in">module</span>:&#123;</span><br><span class="line">        rules:[</span><br><span class="line">            &#123;<span class="attr">test</span>:<span class="regexp">/\.js|jsx/</span>,use:<span class="string">&#x27;babel-loader&#x27;</span>,<span class="attr">exclude</span>:<span class="regexp">/node_modules/</span>&#125;,</span><br><span class="line">            <span class="comment">//这里就是相当于给我们的css文件配置了模块作用域</span></span><br><span class="line">            &#123;<span class="attr">test</span>:<span class="regexp">/\.css/</span>,use:[<span class="string">&#x27;style-loader&#x27;</span>,<span class="string">&#x27;css-loader?modules&#x27;</span>]&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">        </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//开启了模块作用域之后 我们可以看到 打印出来的css文件就是一个对象</span></span><br><span class="line"><span class="keyword">import</span> cssObj <span class="keyword">from</span> <span class="string">&#x27;@/css/cmitem.css&#x27;</span></span><br><span class="line"><span class="built_in">console</span>.log(cssObj)</span><br><span class="line"><span class="comment">//打印如下 下面就是你写的类名</span></span><br><span class="line"><span class="built_in">Object</span></span><br><span class="line">content: <span class="string">&quot;_3lUKvY3y-3umWYdseLKJfW&quot;</span></span><br><span class="line">item: <span class="string">&quot;Fc4cHN8oGjWotN7k930rf&quot;</span></span><br><span class="line">user: <span class="string">&quot;_6cxUR1gNCL_8LlQzfDm2&quot;</span></span><br><span class="line">__proto__: <span class="built_in">Object</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">CmItem</span> (<span class="params">props</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//在使用的时候就可以这么写了</span></span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;cssObj.item&#125;</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">h1</span> <span class="attr">className</span>=<span class="string">&#123;cssObj.user&#125;</span>&gt;</span>评论人:&#123;props.user&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">p</span> <span class="attr">className</span>=<span class="string">&#123;cssObj.content&#125;</span>&gt;</span>评论内容:&#123;props.content&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="css模块化自定义规则"><a href="#css模块化自定义规则" class="headerlink" title="css模块化自定义规则"></a>css模块化自定义规则</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>上面我们看到了 css开启模块化之后 打印出来的对象里面的值</span><br><span class="line"><span class="number">2.</span>其实我们也可以使用 localIdenName  自定义命名的规则</span><br><span class="line"><span class="number">3.</span>[path] 表示样式表 相对于项目根目录 所在的路径</span><br><span class="line">[name] 表示 样式标名文件的名称</span><br><span class="line">[local] 表示 样式的类名定义名称</span><br><span class="line">[hash:length] 表示<span class="number">32</span>位的hash值</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//例子 自定义了我们的类名</span></span><br><span class="line">&#123;<span class="attr">tset</span>:<span class="string">&#x27;/\.css/&#x27;</span>,<span class="attr">use</span>:[<span class="string">&#x27;style-loader&#x27;</span>,<span class="string">&#x27;css-laoder?modules&amp;localIdenName=[path][name]-[loacal]-[hash:5]&#x27;</span>]&#125;</span><br></pre></td></tr></table></figure>
<h3 id="高版本的css-loader配置"><a href="#高版本的css-loader配置" class="headerlink" title="高版本的css-loader配置"></a>高版本的css-loader配置</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> htmlPlugin = <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">    title:<span class="string">&#x27;React学习&#x27;</span>,</span><br><span class="line">    inject:<span class="literal">true</span>,</span><br><span class="line">    template:path.join(__dirname,<span class="string">&#x27;./src/index.html&#x27;</span>),</span><br><span class="line">    filename:<span class="string">&#x27;index.html&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    plugins:[</span><br><span class="line">        htmlPlugin</span><br><span class="line">    ],</span><br><span class="line">    resolve:&#123;</span><br><span class="line">        <span class="comment">//后缀名称可以不写</span></span><br><span class="line">        extensions:[<span class="string">&#x27;.js&#x27;</span>,<span class="string">&#x27;.jsx&#x27;</span>,<span class="string">&#x27;.json&#x27;</span>],</span><br><span class="line">        alias: &#123;</span><br><span class="line">            <span class="string">&#x27;@&#x27;</span>:path.resolve(__dirname,<span class="string">&#x27;./src&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//第三方模块的配置规则</span></span><br><span class="line">    <span class="built_in">module</span>:&#123;</span><br><span class="line">        rules:[</span><br><span class="line">            <span class="comment">//以js解为 或者 jsx 结尾的 使用 babel-loader去转换 排除 node_modules下的js文件</span></span><br><span class="line">            &#123;<span class="attr">test</span>:<span class="regexp">/\.js|jsx/</span>,use:<span class="string">&#x27;babel-loader&#x27;</span>,<span class="attr">exclude</span>:<span class="regexp">/node_modules/</span>&#125;,</span><br><span class="line">            <span class="comment">//这里loader的加载顺序 一定式重右向左</span></span><br><span class="line">            &#123;<span class="attr">test</span>:<span class="regexp">/\.css/</span>,use:[<span class="string">&#x27;style-loader&#x27;</span>,&#123;</span><br><span class="line">                loader:<span class="string">&#x27;css-loader&#x27;</span>,</span><br><span class="line">                options:&#123;</span><br><span class="line">                    modules: &#123;</span><br><span class="line">                        mode: <span class="string">&#x27;local&#x27;</span>,</span><br><span class="line">                        localIdentName: <span class="string">&#x27;[path][name]__[local]--[hash:base64:5]&#x27;</span>,</span><br><span class="line">                      &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;]&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">        </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="css中的全局"><a href="#css中的全局" class="headerlink" title="css中的全局"></a>css中的全局</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>开启了css 模块化之后 我们单独写的css文件就是一个模块 </span><br><span class="line"><span class="number">2.</span>如果我们 有一个单独的样式类名 希望它是全局的 而又在这个css模块里面 我们可以使用 :<span class="function"><span class="title">global</span>(<span class="params">.test</span>)</span>&#123;font-size:italic&#125; 这种语法去包括 此时 这个类样式 就不在这个模块中了</span><br></pre></td></tr></table></figure>
<h3 id="css中的一个元素添加多个类名的方式"><a href="#css中的一个元素添加多个类名的方式" class="headerlink" title="css中的一个元素添加多个类名的方式"></a>css中的一个元素添加多个类名的方式</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>可以采用类名拼接的方式去写</span><br><span class="line"><span class="number">2.</span>也可以采用数组的的方式去写</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> cssObj <span class="keyword">from</span> <span class="string">&#x27;@/css/cmitem.css&#x27;</span></span><br><span class="line"><span class="built_in">console</span>.log(cssObj)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">CmItem</span> (<span class="params">props</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//这里采用拼接空格 来为一个元素添加多个类名</span></span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;cssObj.item</span> +&#x27; &#x27;+ <span class="attr">cssObj.itemColor</span>&#125;&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">h1</span> <span class="attr">className</span>=<span class="string">&#123;cssObj.user&#125;</span>&gt;</span>评论人:&#123;props.user&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">p</span> <span class="attr">className</span>=<span class="string">&#123;cssObj.content&#125;</span>&gt;</span>评论内容:&#123;props.content&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> cssObj <span class="keyword">from</span> <span class="string">&#x27;@/css/cmitem.css&#x27;</span></span><br><span class="line"><span class="built_in">console</span>.log(cssObj)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">CmItem</span> (<span class="params">props</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//采用数组的join方法来切割</span></span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;[cssObj.item,cssObj.itemColor].join(</span>&#x27; &#x27;)&#125;&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">h1</span> <span class="attr">className</span>=<span class="string">&#123;cssObj.user&#125;</span>&gt;</span>评论人:&#123;props.user&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">p</span> <span class="attr">className</span>=<span class="string">&#123;cssObj.content&#125;</span>&gt;</span>评论内容:&#123;props.content&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="React中的事件绑定"><a href="#React中的事件绑定" class="headerlink" title="React中的事件绑定"></a>React中的事件绑定</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>React中 有一套自己的事件绑定机制 采用驼峰</span><br><span class="line"><span class="number">2.</span> &lt;button onClick=&#123;一个函数&#125;&gt;按钮&lt;/button&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//React中 事件里面是一个函数 </span></span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    &lt;button onClick=&#123;<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="built_in">console</span>.log(<span class="number">111</span>)&#125;&#125;&gt;按钮&lt;/button&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//class组件可以通过条用自己的方法 去产生事件处理函数</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyEvent</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//这里通过调用自己的方法 来传递事件处理函数</span></span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;this.say&#125;</span>&gt;</span>按钮<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">say</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="number">111</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    &lt;MyEvent/&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//目前最规范的语法 这样写还可以传入参数 </span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyEvent</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">//写一个箭头函数 然后里面写要函数的调用 即可传入参数</span></span><br><span class="line">        <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> &#123;this.say(&#x27;我是传入的参数&#x27;)&#125;&#125;&gt;按钮<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">say</span>(<span class="params">str</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(str)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    &lt;MyEvent/&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h2 id="React中的文本框绑定"><a href="#React中的文本框绑定" class="headerlink" title="React中的文本框绑定"></a>React中的文本框绑定</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>在React中没有 指令的概念 也就是说我们使用不了 v-model 这种的双向绑定</span><br><span class="line"><span class="number">2.</span>所以在react中  我们要自己去写文本框 的数据绑定 利用绑定文本框的value 和 它的change 来处理</span><br><span class="line"><span class="number">3.</span>我们需要手动监听onChange事件 然后 拿到文本框最新的值 然后在用<span class="built_in">this</span>.setState()去设置</span><br><span class="line"><span class="number">4.</span>获取文本框的当前值 有两种方案 一种是通过传入事件处理函数 e  第二种是 使用ref</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//第一种 通过事件处理函数e 拿到 当前文本框的值</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyEvent</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">        <span class="built_in">this</span>.state =&#123;</span><br><span class="line">            msg:<span class="string">&#x27;我是state中的数据&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> &lt;div&gt;</span><br><span class="line">        &lt;button onClick=&#123;<span class="function">() =&gt;</span> &#123;<span class="built_in">this</span>.say(<span class="string">&#x27;我是传入的参数&#x27;</span>)&#125;&#125;&gt;按钮&lt;/button&gt;</span><br><span class="line">        &lt;div&gt;&#123;<span class="built_in">this</span>.state.msg&#125;&lt;/div&gt;</span><br><span class="line">        <span class="comment">/* 在调用onChange的时候 把事件处理函数 中的e传入进去 然后通过拿到e.target.value 拿到值 */</span></span><br><span class="line">        &lt;input type=<span class="string">&quot;text&quot;</span> value=&#123;<span class="built_in">this</span>.state.msg&#125; onChange=&#123;<span class="function">(<span class="params">e</span>) =&gt;</span> &#123; <span class="built_in">this</span>.model(e) &#125;&#125;&gt;&lt;/input&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">say</span>(<span class="params">str</span>)</span>&#123;</span><br><span class="line">       <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">           msg:<span class="string">&#x27;我被改变了🐖 🍕&#x27;</span></span><br><span class="line">       &#125;,<span class="function">() =&gt;</span> &#123;</span><br><span class="line">           <span class="built_in">console</span>.log(<span class="built_in">this</span>.state.msg)</span><br><span class="line">       &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">model</span>(<span class="params">e</span>)</span>&#123;</span><br><span class="line">        <span class="comment">/* 在调用onChange的时候 把事件处理函数 中的e传入进去 然后通过拿到e.target.value 拿到值 */</span></span><br><span class="line">        <span class="built_in">console</span>.log(e.target.value)</span><br><span class="line">        <span class="comment">/* 载用setState 去 设置  */</span></span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            msg:e.target.value</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    &lt;MyEvent/&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//第二种 通过this.ref.value</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyEvent</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">        <span class="built_in">this</span>.state =&#123;</span><br><span class="line">            msg:<span class="string">&#x27;我是state中的数据&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> &lt;div&gt;</span><br><span class="line">        &lt;button onClick=&#123;<span class="function">() =&gt;</span> &#123;<span class="built_in">this</span>.say(<span class="string">&#x27;我是传入的参数&#x27;</span>)&#125;&#125;&gt;按钮&lt;/button&gt;</span><br><span class="line">        &lt;div&gt;&#123;<span class="built_in">this</span>.state.msg&#125;&lt;/div&gt;</span><br><span class="line">        绑定ref text</span><br><span class="line">        &lt;input type=<span class="string">&quot;text&quot;</span> value=&#123;<span class="built_in">this</span>.state.msg&#125; onChange=&#123;<span class="function">(<span class="params">e</span>) =&gt;</span> &#123; <span class="built_in">this</span>.model(e) &#125;&#125; ref=<span class="string">&quot;text&quot;</span>&gt;&lt;/input&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">say</span>(<span class="params">str</span>)</span>&#123;</span><br><span class="line">       <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">           msg:<span class="string">&#x27;我被改变了🐖 🍕&#x27;</span></span><br><span class="line">       &#125;,<span class="function">() =&gt;</span> &#123;</span><br><span class="line">           <span class="built_in">console</span>.log(<span class="built_in">this</span>.state.msg)</span><br><span class="line">       &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">model</span>(<span class="params">e</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//通过this.refs.value 拿到 文本框的值</span></span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.refs.text.value)</span><br><span class="line">	    <span class="comment">//然后在通过setState中去设置</span></span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            msg:<span class="built_in">this</span>.refs.text.value</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mydiv = &lt;div&gt;</span><br><span class="line">    &lt;MyEvent/&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">ReactDOM.render(mydiv,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h2 id="create-react-app-脚手架的使用"><a href="#create-react-app-脚手架的使用" class="headerlink" title="create react app 脚手架的使用"></a>create react app 脚手架的使用</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>需要全局安装 npm install -g create-react-app</span><br><span class="line"><span class="number">2.</span>可以通过 create-react-app -v 查看版本或者安装是否成功</span><br><span class="line"><span class="number">3.</span>创建项目 create-react-app 项目名称</span><br></pre></td></tr></table></figure>
<h2 id="在-create-react-app-配置跨域代理"><a href="#在-create-react-app-配置跨域代理" class="headerlink" title="在 create react app 配置跨域代理"></a>在 create react app 配置跨域代理</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们这里的数据由于使用的是 json-server 所用需要跨域 详情的 json-server 笔记</span><br><span class="line"><span class="number">2.</span>首先我们需要在node_modules 文件夹下找到 react-scripts 文件夹 下面的 config文件夹 就可以看到webpackDevServer.config文件</span><br><span class="line"><span class="number">3.</span>然后添加 proxy 配置项</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">//配置如下</span></span><br><span class="line">publicPath: paths.publicUrlOrPath.slice(<span class="number">0</span>, -<span class="number">1</span>),</span><br><span class="line">proxy:&#123;</span><br><span class="line">  <span class="string">&quot;/api&quot;</span>:&#123;</span><br><span class="line">    target:<span class="string">&quot;http://localhost:4000&quot;</span>,</span><br><span class="line">    changeOrigin:<span class="literal">true</span>,</span><br><span class="line">    <span class="string">&quot;pathRewrite&quot;</span>:&#123;</span><br><span class="line">      <span class="string">&quot;^/api&quot;</span>:<span class="string">&quot;/&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h2 id="react-多行标签"><a href="#react-多行标签" class="headerlink" title="react 多行标签"></a>react 多行标签</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们知道 vue中没个元素 只能由一个根节点 也就是 一个组件中需要由一个元素去包裹 不能写多行标签</span><br><span class="line"><span class="number">2.</span>React中 如果我们需要写多行 标签的化 可以由两种方式</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用空标签的形式</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Hellow</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>我是一<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="xml">    	    <span class="tag">&lt;<span class="name">div</span>&gt;</span>我是二<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="xml">    	   <span class="tag">&lt;/&gt;</span></span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//使用react中 Fragment</span></span><br><span class="line"><span class="keyword">import</span> React,&#123;Component,Fragment&#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Hellow</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> &lt;Fragment&gt;</span><br><span class="line">            &lt;div&gt;我是一&lt;/div&gt;</span><br><span class="line">    	    &lt;div&gt;我是二&lt;/div&gt;</span><br><span class="line">    	   &lt;/Fragment&gt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="React中的路由的使用"><a href="#React中的路由的使用" class="headerlink" title="React中的路由的使用"></a>React中的路由的使用</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先需要安装 react-router-dom 这个包 npm install renact-router-dom -S 这个依赖包的功能会更多一些</span><br><span class="line"><span class="number">2.</span>路由分两种 一种react-router 只提供了一些核心的API react-router-dom 提供了更多的选项</span><br><span class="line"><span class="number">3.</span>路由模式分两种 </span><br><span class="line">一种是hash模式 带# 刷新的时候页面不会丢失 </span><br><span class="line">一种是 BrowserRouter 历史记录模式 没有# 刷新会丢失 本地模式不会</span><br><span class="line"><span class="number">4.</span>首先需要在入口文件中 引入 react-router-dom 然后确定那种模式</span><br><span class="line"><span class="number">5.</span>然后 用你引入的模式 把我们的根标签包裹起来</span><br></pre></td></tr></table></figure>
<h2 id="Route写法"><a href="#Route写法" class="headerlink" title="Route写法"></a>Route写法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>Route 相当于 Vue中的 component 标签  path属性 是路径 component属性 相当于is 就是你要渲染的组件</span><br><span class="line"><span class="number">2.</span>首先我们需要用哪种路由模式 去包裹我们的根组件</span><br><span class="line"><span class="number">3.</span>然后在相关的根组件里 去引入 我们的route 然后写入 对应的路径 和 需要渲染的组件</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//入口文件 index.js</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">&#x27;react-dom&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./index.css&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> reportWebVitals <span class="keyword">from</span> <span class="string">&#x27;./reportWebVitals&#x27;</span>;</span><br><span class="line"><span class="comment">//引入路由的模式  这里也可以引入 HashRouter </span></span><br><span class="line"><span class="keyword">import</span> &#123;BrowserRouter&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//使用标签的形式 把你的根组件包裹起来 然后用HashRouter 标签的形式 把根组件包裹起来</span></span><br><span class="line">ReactDOM.render(</span><br><span class="line">  &lt;BrowserRouter&gt;</span><br><span class="line">  &lt;React.StrictMode&gt;</span><br><span class="line">    &lt;App /&gt;</span><br><span class="line">  &lt;/React.StrictMode&gt;</span><br><span class="line">  &lt;/BrowserRouter&gt;,</span><br><span class="line">  <span class="built_in">document</span>.getElementById(<span class="string">&#x27;root&#x27;</span>)</span><br><span class="line">);</span><br><span class="line">reportWebVitals();</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//在根组件中 引入 对应的组件 引入 Route 然后写入 需要渲染的组件 和路径</span></span><br><span class="line"><span class="comment">//引入Route 组件</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure>
<p><img src="2.gif" alt=""></p>
<h3 id="路由导航-Link"><a href="#路由导航-Link" class="headerlink" title="路由导航 Link"></a>路由导航 Link</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>相当于 我们vue中的router-link 通过按钮的形式 去想去的组件 在React中 组件是Link 需要从react-router-dom中引入</span><br><span class="line"><span class="number">2.</span>通过 to 属性 指定要去的组件</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//引入了link 标签 </span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      &#123;<span class="comment">/*相当于vue中的router-link 指定路径 点击去路径匹配的组件*/</span>&#125;   </span><br><span class="line">      &lt;Link to=<span class="string">&quot;/coma&quot;</span>&gt;去coma&lt;/Link&gt;</span><br><span class="line">      &lt;Link to=<span class="string">&quot;/comb&quot;</span>&gt;去comb&lt;/Link&gt;</span><br><span class="line">      &lt;Link to=<span class="string">&quot;/comc&quot;</span>&gt;去comc&lt;/Link&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure>
<p><img src="3.gif" alt=""></p>
<h3 id="路由导航-NavLink"><a href="#路由导航-NavLink" class="headerlink" title="路由导航 NavLink"></a>路由导航 NavLink</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>使用方式和 Link 一样 需要从react-router-dom 中引入</span><br><span class="line"><span class="number">2.</span>和link 的区别 会默认 给我们点击的当前 项加一个<span class="class"><span class="keyword">class</span> <span class="title">active</span>的类</span></span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//NavLink</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/coma&quot;</span>&gt;去coma&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comb&quot;</span>&gt;去comb&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comc&quot;</span>&gt;去comc&lt;/NavLink&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure>
<p><img src="4.gif" alt=""></p>
<h3 id="精准匹配-exact"><a href="#精准匹配-exact" class="headerlink" title="精准匹配  exact"></a>精准匹配  exact</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们知道 我们的路由路径 都是以 / 斜杠什么的而开头 </span><br><span class="line"><span class="number">2.</span>但是当我们的 路由路径只有斜杠的时候 只要我们访问以斜杠开头的路径 都会匹配到</span><br><span class="line"><span class="number">3.</span>所以这里就有了精准匹配 在route 的标签里 添加exact属性</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/coma&quot;</span>&gt;去coma&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comb&quot;</span>&gt;去comb&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comc&quot;</span>&gt;去comc&lt;/NavLink&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      <span class="comment">//这里 加了exact 只有 刚问 / 的路由地址的时候 才会显示了这个组件</span></span><br><span class="line">      &lt;Route path=<span class="string">&quot;/&quot;</span> exact component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure>
<h3 id="路由的唯一渲染-Switch标签"><a href="#路由的唯一渲染-Switch标签" class="headerlink" title="路由的唯一渲染 Switch标签"></a>路由的唯一渲染 Switch标签</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>当我们有多个route标签 去渲染同一个组件的时候 会都渲染出来</span><br><span class="line"><span class="number">2.</span>这里为了 同一路径的 同一个组件 因为重复的route标签 重复渲染的情况 所以有了Swich标签</span><br><span class="line"><span class="number">3.</span><span class="keyword">switch</span> 标签 可以确保 当你有重复的路由地址 或者组件的化 只渲染一个</span><br><span class="line"><span class="number">4.</span>需要用<span class="keyword">switch</span>标签 把我们的Route标签包裹起来</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink,Switch&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/coma&quot;</span>&gt;去coma&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comb&quot;</span>&gt;去comb&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comc&quot;</span>&gt;去comc&lt;/NavLink&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">       &#123;<span class="comment">/*如果没有Switch标签的包裹 那么 ComC这个组件都会被重复渲染 */</span>&#125;</span><br><span class="line">      &lt;Switch&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/&quot;</span> exact component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;/Switch&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure>
<h3 id="路由的重定向-Redirect-标签"><a href="#路由的重定向-Redirect-标签" class="headerlink" title="路由的重定向 Redirect 标签"></a>路由的重定向 Redirect 标签</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>当你当问 一个路径的之后  我把它重定向到别的路径</span><br><span class="line"><span class="number">2.</span> 使用Redirect标签 即可</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink,Switch,Redirect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/coma&quot;</span>&gt;去coma&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comb&quot;</span>&gt;去comb&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comc&quot;</span>&gt;去comc&lt;/NavLink&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      &lt;Switch&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &#123;<span class="comment">/* 这里 就是 当我们要去 &#x27;/&#x27; 路径的时候 我们把它重定向到&#x27;/comc&#x27;路径 然后去渲染 &#x27;/comc&#x27;所对应的组件 */</span>&#125;</span><br><span class="line">      &lt;Redirect <span class="keyword">from</span>=<span class="string">&#x27;/&#x27;</span> to=<span class="string">&quot;/comc&quot;</span> exact&gt;&lt;/Redirect&gt;</span><br><span class="line">      &lt;/Switch&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure>
<h3 id="二级路由"><a href="#二级路由" class="headerlink" title="二级路由"></a>二级路由</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们刚刚看到了一级的路由 点击按钮 显示对应的组件 </span><br><span class="line"><span class="number">2.</span>二级路由就是 我们在一级路由显示完毕之后  像是你对应当前组件下的 匹配的组件</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//componentC.jsx</span></span><br><span class="line"><span class="comment">//当我一级路由切换到componentC 组件的时候 它下面又设置了二级路由 路径是的第一位 是我们一级路由的匹配路径 </span></span><br><span class="line"><span class="keyword">import</span> React ,&#123;Component&#125;<span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span>&#123;Route,NavLink&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入二级路由对应要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComcA <span class="keyword">from</span> <span class="string">&#x27;../comc/comca.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComcB <span class="keyword">from</span> <span class="string">&#x27;../comc/comcb.jsx&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">ComC</span> <span class="keyword">extends</span> <span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div&gt;</span><br><span class="line">            &lt;div&gt;我是COMC&lt;/div&gt;</span><br><span class="line">            &lt;NavLink to=<span class="string">&quot;/comc/comca&quot;</span>&gt;去comcA&lt;/NavLink&gt;</span><br><span class="line">            &lt;NavLink to=<span class="string">&quot;/comc/comcb&quot;</span>&gt;去comcB&lt;/NavLink&gt;</span><br><span class="line">            &lt;Route path=<span class="string">&#x27;/comc/comca&#x27;</span> component=&#123;ComcA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">            &lt;Route path=<span class="string">&#x27;/comc/comcb&#x27;</span> component=&#123;ComcB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">            也可以使用渲染函数 去渲染组件    </span><br><span class="line">            &lt;Route path=<span class="string">&#x27;/comc/comcb&#x27;</span> render=&#123;<span class="function">() =&gt;</span> &#123;<span class="keyword">return</span> (&lt;ComcC&gt;)&#125;&#125;&gt;&lt;/Route&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">           </span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink,Switch,Redirect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入一级路由需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="comment">//这里是一级路由的文件  可以看到 当访问到/的时候 我们让他去显示二级路由下的 /comc/comca组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/coma&quot;</span>&gt;去coma&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comb&quot;</span>&gt;去comb&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comc&quot;</span>&gt;去comc&lt;/NavLink&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      &lt;Switch&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Redirect <span class="keyword">from</span>=<span class="string">&#x27;/&#x27;</span> to=<span class="string">&quot;/comc/comca&quot;</span> exact&gt;&lt;/Redirect&gt;</span><br><span class="line">      &lt;/Switch&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//下面的动图可以看到 当我们点击 一级路由按钮的时候 它只显示 一级路由对应的组件 当切换到comc的时候 这里又二级路由所以在点击的时候 我们就在comc组件下面 显示到了二级路由对应的组件 当我们输入重定向的时候 它就去到了 /comc/comca 这个路径下面所匹配的组件</span></span><br></pre></td></tr></table></figure>
<p><img src="5.gif" alt=""></p>
<h3 id="高阶组件-withRouter"><a href="#高阶组件-withRouter" class="headerlink" title="高阶组件  withRouter"></a>高阶组件  withRouter</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>当我们的组件 不是路由切换的组件也具有路由切换的组件的三个属性 </span><br><span class="line"><span class="number">2.</span>比如我们上面的comc组件 是用路由切换过去的组件 那么它上面无论你传值没传值 就会有三个属性 location math history 这三个属性</span><br><span class="line"><span class="number">3.</span>比如我们的根组件 不是用路由切换的 那么 它上面就没有location math history这三个属性</span><br><span class="line"><span class="number">4.</span>所以我们为了能使用这三个属性 就需要用 withRouter 把它包裹起来 那么它上面的props就会有这三个属性</span><br><span class="line"><span class="number">5.</span>这种就叫做高阶组件 HOC 参数是一个组件 返回也是一个组件</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink,Switch,Redirect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="comment">//这里打印出来 是空对象 因为 我们没有用 WithRouter 包裹起来</span></span><br><span class="line">  <span class="built_in">console</span>.log(props)</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/coma&quot;</span>&gt;去coma&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comb&quot;</span>&gt;去comb&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comc&quot;</span>&gt;去comc&lt;/NavLink&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      &lt;Switch&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Redirect <span class="keyword">from</span>=<span class="string">&#x27;/&#x27;</span> to=<span class="string">&quot;/comc/comca&quot;</span> exact&gt;&lt;/Redirect&gt;</span><br><span class="line">      &lt;/Switch&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用WithRouter包裹</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink,Switch,Redirect, withRouter&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="comment">//我们在下面使用withRouter包裹 可以看到 的props上面就增加了</span></span><br><span class="line">    location math history 这三个属性</span><br><span class="line">  <span class="built_in">console</span>.log(props)</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/coma&quot;</span>&gt;去coma&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comb&quot;</span>&gt;去comb&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comc&quot;</span>&gt;去comc&lt;/NavLink&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      &lt;Switch&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Redirect <span class="keyword">from</span>=<span class="string">&#x27;/&#x27;</span> to=<span class="string">&quot;/comc/comca&quot;</span> exact&gt;&lt;/Redirect&gt;</span><br><span class="line">      &lt;/Switch&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(App);</span><br></pre></td></tr></table></figure>
<h3 id="编程式导航"><a href="#编程式导航" class="headerlink" title="编程式导航"></a>编程式导航</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们有了高阶组件之后 就可以使用它对应的api 可以在那三个属性里 拿到对应的值去使用</span><br><span class="line"><span class="number">2.</span>如果编程式导航 我们就可以使用history.push 方法  去到我们想去的组件</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink,Switch,Redirect, withRouter&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(props)</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/coma&quot;</span>&gt;去coma&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comb&quot;</span>&gt;去comb&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comc&quot;</span>&gt;去comc&lt;/NavLink&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      &#123;<span class="comment">/*这里我们把APP改成了高阶组件 就可以使用 history里的push方法 去做编程式导航 */</span>&#125;</span><br><span class="line">      &lt;button onClick=&#123;<span class="function">() =&gt;</span> &#123;props.history.push(<span class="string">&quot;/comc&quot;</span>)&#125;&#125;&gt;去comca二级路由&lt;/button&gt;</span><br><span class="line">      &lt;Switch&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Redirect <span class="keyword">from</span>=<span class="string">&#x27;/&#x27;</span> to=<span class="string">&quot;/comc/comca&quot;</span> exact&gt;&lt;/Redirect&gt;</span><br><span class="line">      &lt;/Switch&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(App);</span><br></pre></td></tr></table></figure>
<h3 id="路由传参"><a href="#路由传参" class="headerlink" title="路由传参"></a>路由传参</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>有的我们在页面跳转的时候 需要传递对应的参数</span><br><span class="line"><span class="number">2.</span>比如我们在列表 跳详情的时候 就需要传递参数</span><br><span class="line"><span class="number">3.</span>传参有两种 一种是使用 params 在路由规则中设置传递参数</span><br><span class="line">二种是 直接在路径种传递</span><br><span class="line"><span class="number">4.</span>接收参数 props.match,params 参数名</span><br><span class="line"><span class="number">5.</span>params传参 刷新地址 参数依然存在 只能传递字符串</span><br><span class="line"><span class="number">6.</span>query传参 不需要在路由规则中 匹配 直接发送数据 使用<span class="built_in">this</span>.props.location.query接收</span><br></pre></td></tr></table></figure>
<h3 id="params传参"><a href="#params传参" class="headerlink" title="params传参"></a>params传参</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//首先路由匹配的规则里 加动态参数 然后点击的时候 传参</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink,Switch,Redirect, withRouter&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(props)</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/coma&quot;</span>&gt;去coma&lt;/NavLink&gt;</span><br><span class="line">      &#123;<span class="comment">/* 当点击按钮的时候 就传了一个参数过去 */</span>&#125;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comb/我是传过来的ID&quot;</span>&gt;去comb&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comc&quot;</span>&gt;去comc&lt;/NavLink&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      &lt;button onClick=&#123;<span class="function">() =&gt;</span> &#123;props.history.push(<span class="string">&quot;/comc&quot;</span>)&#125;&#125;&gt;去comca二级路由&lt;/button&gt;</span><br><span class="line">      &lt;Switch&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &#123;<span class="comment">/* 当然我们这里 需要用动态路由匹配一下参数 */</span>&#125;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb/:id&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Redirect <span class="keyword">from</span>=<span class="string">&#x27;/&#x27;</span> to=<span class="string">&quot;/comc/comca&quot;</span> exact&gt;&lt;/Redirect&gt;</span><br><span class="line">      &lt;/Switch&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(App);</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//然后我们在需要接收参数的组件里 使用this.props.match.params.id接收 </span></span><br><span class="line"><span class="keyword">import</span> React ,&#123;Component&#125;<span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">ComB</span> <span class="keyword">extends</span> <span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">componentDidMount</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">//这里在生命周期函数里 打印了出来  这里就是打印的就是你接收的参数</span></span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.props.match.params.id)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div&gt;我是COMB&lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="query传参"><a href="#query传参" class="headerlink" title="query传参"></a>query传参</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//query传参  就去要把按钮重新改写一下</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink,Switch,Redirect, withRouter&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(props)</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      <span class="comment">//需要这样传参 使用括号包裹起来 然后打上query关键字 </span></span><br><span class="line">      &lt;NavLink to=&#123;&#123;<span class="attr">pathname</span>:<span class="string">&#x27;/coma&#x27;</span>,<span class="attr">query</span>:&#123;<span class="attr">name</span>:<span class="string">&#x27;zs&#x27;</span>&#125;&#125;&#125;&gt;去coma&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comb/我是传过来的ID&quot;</span>&gt;去comb&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comc&quot;</span>&gt;去comc&lt;/NavLink&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      &lt;button onClick=&#123;<span class="function">() =&gt;</span> &#123;props.history.push(<span class="string">&quot;/comc&quot;</span>)&#125;&#125;&gt;去comca二级路由&lt;/button&gt;</span><br><span class="line">      &lt;Switch&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb/:id&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Redirect <span class="keyword">from</span>=<span class="string">&#x27;/&#x27;</span> to=<span class="string">&quot;/comc/comca&quot;</span> exact&gt;&lt;/Redirect&gt;</span><br><span class="line">      &lt;/Switch&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(App);</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//coma组件在接收的时候 使用this.props.location.query</span></span><br><span class="line"><span class="keyword">import</span> React ,&#123;Component&#125;<span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">ComA</span> <span class="keyword">extends</span> <span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">componentDidMount</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.props.location.query,<span class="string">&#x27;-----coma&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div&gt;我是COMA&lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//剩下的一些按钮之间的传递的参数语法</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink,Switch,Redirect, withRouter&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="comment">//把需要渲染的组件 用 Route标签 包裹起来 写入组件渲染的路径 和需要渲染的组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">      &lt;NavLink to=&#123;&#123;<span class="attr">pathname</span>:<span class="string">&#x27;/coma&#x27;</span>,<span class="attr">query</span>:&#123;<span class="attr">name</span>:<span class="string">&#x27;zs&#x27;</span>&#125;&#125;&#125;&gt;去coma&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comb/我是传过来的ID&quot;</span>&gt;去comb&lt;/NavLink&gt;</span><br><span class="line">      &lt;NavLink to=<span class="string">&quot;/comc/我是navlink传过来的&quot;</span>&gt;去comc&lt;/NavLink&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">      &lt;button onClick=&#123;<span class="function">() =&gt;</span> &#123;props.history.push(<span class="string">&quot;/comc&quot;</span>)&#125;&#125;&gt;去comca二级路由&lt;/button&gt;</span><br><span class="line">      &lt;button onClick=&#123;<span class="function">() =&gt;</span> &#123;props.history.push(&#123;<span class="attr">pathname</span>:<span class="string">&#x27;/coma&#x27;</span>,<span class="attr">query</span>:&#123;<span class="attr">name</span>:<span class="string">&#x27;我是按钮传过来的参数&#x27;</span>&#125;&#125;)&#125;&#125;&gt;去comca&lt;/button&gt;</span><br><span class="line">      &lt;button onClick=&#123;<span class="function">() =&gt;</span> &#123;props.history.push(<span class="string">&quot;/comc/我是按钮传过来的ID&quot;</span>)&#125;&#125;&gt;去comcc&lt;/button&gt;</span><br><span class="line">      &lt;Switch&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/coma&quot;</span> component=&#123;ComA&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comb/:id&quot;</span> component=&#123;ComB&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc/:id&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Route path=<span class="string">&quot;/comc&quot;</span> component=&#123;ComC&#125;&gt;&lt;/Route&gt;</span><br><span class="line">      &lt;Redirect <span class="keyword">from</span>=<span class="string">&#x27;/&#x27;</span> to=<span class="string">&quot;/comc/comca&quot;</span> exact&gt;&lt;/Redirect&gt;</span><br><span class="line">      &lt;/Switch&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(App);</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="Hook"><a href="#Hook" class="headerlink" title="Hook"></a>Hook</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>HOOK 是React <span class="number">16.7</span> 新增的属性  让无状态组件 使用状态</span><br><span class="line"><span class="number">2.</span>在react中 状态的管理是必不可少的</span><br><span class="line"><span class="number">3.</span>使用的时候 需要引入 useState <span class="keyword">import</span> &#123;useState&#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="number">4.</span>我们需要调用useState 它返回的是一个数组 第一个事当前的状态值 第二个是对象 表明用于更改状态的函数(类似于setState)</span><br><span class="line"><span class="number">5.</span>useState 解构出来 两个参数 第一个参数就是你声明的值 第二个就是当你要修改这个值 所用到的api</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span></span><br><span class="line"><span class="comment">//引入对应的useState hook </span></span><br><span class="line"><span class="keyword">import</span> &#123;useState&#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route,Link,NavLink,Switch,Redirect, withRouter&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入需要渲染的组件</span></span><br><span class="line"><span class="keyword">import</span> ComA <span class="keyword">from</span> <span class="string">&#x27;./components/componentA.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComB <span class="keyword">from</span> <span class="string">&#x27;./components/componentB.jsx&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ComC <span class="keyword">from</span> <span class="string">&#x27;./components/componentC.jsx&#x27;</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="comment">//useState 是你声明的值 解构出来的val 是你的声明的值 setVal类似于setState 当修改的时候使用setVal  </span></span><br><span class="line">  <span class="keyword">let</span> [val,setVal] = useState(<span class="string">&#x27;我是useState&#x27;</span>)</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">      &#123;val&#125;</span><br><span class="line">      &#123;<span class="comment">/* 使用 */</span>&#125;</span><br><span class="line">      &lt;button onClick=&#123;<span class="function">() =&gt;</span>&#123;setVal(val+<span class="string">&#x27;1111&#x27;</span>)&#125;&#125;&gt;修改状态&lt;/button&gt;  </span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(App);</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//我们可以使用传入对象的形式 去定义多个数据</span></span><br><span class="line">unction <span class="function"><span class="title">App</span>(<span class="params">props</span>)</span> &#123;</span><br><span class="line">  <span class="keyword">let</span> [val,setVal] = useState(&#123;</span><br><span class="line">    name:<span class="number">0</span>,</span><br><span class="line">    age:<span class="number">13</span></span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      我是公共部分</span><br><span class="line">     &lt;div&gt;</span><br><span class="line">      &#123;val.age&#125;</span><br><span class="line">      &#123;val.name&#125;</span><br><span class="line">     &lt;/div&gt;</span><br><span class="line">      &lt;button onClick=&#123;<span class="function">() =&gt;</span>&#123;多个状态修改以后在说&#125;&#125;&gt;修改状态&lt;/button&gt;  </span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(App);</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="Redux的使用"><a href="#Redux的使用" class="headerlink" title="Redux的使用"></a>Redux的使用</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>redux js 提供了一个可预测型的的状态容器  我们给一个固定的输入 那么必定可以得到一个结果</span><br><span class="line"><span class="number">2.</span> 集中管理react 中的多个组件的状态 </span><br><span class="line"><span class="number">3.</span>redux 是一个专门的状态管理库 是一个插件 在VUE 中等 也可以使用 </span><br><span class="line"><span class="number">4.</span>需求场景 多个组件的状态需要共享的时候 可以用redux 或者一个组件 需要改变另外一个组件状态的时候 或者组件中的状态在任何地方都可以拿到 数据</span><br><span class="line"><span class="number">5.</span>redux 三大原则 单一数据源 整个react 当中的状态 都会被统一的管理到store中 </span><br><span class="line">redux 中的 数据 是只读的  我们不能够直接改变state 而是要通过触发 redux中 的特定方法  去改变state</span><br><span class="line">redux 使用纯函数 来执行修改(action)</span><br><span class="line"><span class="number">6.</span>安装  npm install redux</span><br><span class="line"><span class="number">7.</span> redux中的state 是只读的 不要直接赋值 我们可以通过返回新对象的形式 去赋值</span><br></pre></td></tr></table></figure>
<h3 id="redux的数据获取"><a href="#redux的数据获取" class="headerlink" title="redux的数据获取"></a>redux的数据获取</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>基本使用 首先创建数 这个数据一定是个函数 然后 返回你的数据</span><br><span class="line"><span class="number">2.</span>然后新建一个js文件  里面 引入 redux 使用createStore函数 把我们刚才创建的那个数据函数导入进去</span><br><span class="line"><span class="number">3.</span>然后 在使用公共数据页面里 引入 我们刚刚创建的那个 js 文件  然后使用getState函数 即可使用数据</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//我们新建一个store文件夹 下面新建一个数据文件 data.js 记住这里创建的数据 是以函数的形式返回的</span></span><br><span class="line"><span class="keyword">const</span> obj = &#123;<span class="attr">name</span>:<span class="string">&#x27;张三&#x27;</span>,<span class="attr">age</span>:<span class="number">18</span>&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> data = <span class="function">(<span class="params">state= obj,action</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> state</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//然后在store 文件夹下面 新建一个 index.js 文件  这个文件用来 创建 redux 并且把我们刚才的那个数据文件 里的数据变成公共的数据管理状态 </span></span><br><span class="line"><span class="comment">//从redux中 导入我们的数据状态</span></span><br><span class="line"><span class="keyword">import</span> &#123; createStore&#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"><span class="comment">//导入我们的数据</span></span><br><span class="line"><span class="keyword">import</span> &#123;data&#125; <span class="keyword">from</span> <span class="string">&#x27;./data.js&#x27;</span></span><br><span class="line"><span class="comment">//把我们的数据变成 公共的数据状态管理</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> store = createStore(data)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//然后在需要 使用到公共状态的 引入我刚刚创建的那个 index.js文件  使用getState来获取数据</span></span><br><span class="line"><span class="comment">//引入我们刚才的那个store</span></span><br><span class="line"><span class="keyword">import</span> &#123;store&#125; <span class="keyword">from</span> <span class="string">&#x27;../store/index.js&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Live</span> <span class="keyword">extends</span> <span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">        <span class="built_in">this</span>.state =&#123;</span><br><span class="line">            opacity:<span class="number">1</span>,</span><br><span class="line">            timerID:<span class="literal">null</span>,</span><br><span class="line">           <span class="comment">// 然后 使用 getState函数 获我们的胡数据文件</span></span><br><span class="line">            name:store.getState()</span><br><span class="line">        &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div&gt;</span><br><span class="line">                &#123;<span class="comment">/*这样我们定义的obj的那个 数据就获取到了*/</span>&#125;</span><br><span class="line">                &lt;div&gt;&#123;<span class="built_in">this</span>.state.name.name&#125;&lt;/div&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="redux的数据修改"><a href="#redux的数据修改" class="headerlink" title="redux的数据修改"></a>redux的数据修改</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们redux 其实 和vue一样 不能直接修改数据 也是动过dispatch函数 发射action 来修改数据</span><br><span class="line"><span class="number">2.</span>当我们使用dispatch 发射action的时候 其实就是触发了 你挂载到 createStore 里面的那个函数 传进去了的第二个形参</span><br><span class="line"><span class="number">3.</span>而我们的公共数据状态改变了 要是页面也要改变的话 需要使用 subscribe 函数 来监听一下 在把数据重新赋值 页面才会改变</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//我们定义的数据函数的第二个参数用来接收 dispatch发送过来的函数 这个函数是带着参数过来的</span></span><br><span class="line"><span class="keyword">const</span> count = <span class="number">20</span></span><br><span class="line"><span class="comment">//这里的数据的函数的第二个参数 就是当你使用store.dispatch触发函数的返回值 详细见下面  </span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> data = <span class="function">(<span class="params">state= count,action</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">switch</span>(action.type)&#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;add&#x27;</span>:</span><br><span class="line">        <span class="keyword">return</span> state + action.data</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">case</span><span class="string">&#x27;del&#x27;</span>:</span><br><span class="line">        <span class="keyword">return</span> state - action.data</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">default</span> :</span><br><span class="line">        <span class="keyword">return</span> state</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// store.dispatch 发送的函数 这个函数的返回值 就是我们上面 data函数里 第二个参数</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> add = <span class="function">(<span class="params">num</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;<span class="attr">type</span>:<span class="string">&#x27;add&#x27;</span>,<span class="attr">data</span>:num&#125; </span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> del = <span class="function">(<span class="params">num</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;<span class="attr">type</span>:<span class="string">&#x27;del&#x27;</span>,<span class="attr">data</span>:num&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//然后在页面中 触发 我们写入的这个函数</span></span><br><span class="line"><span class="keyword">import</span> &#123;store&#125; <span class="keyword">from</span> <span class="string">&#x27;../store/index.js&#x27;</span></span><br><span class="line"><span class="comment">//引入 我们写好的action 函数  就是我们上面的代码</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> action <span class="keyword">from</span> <span class="string">&#x27;../store/action.js&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Live</span> <span class="keyword">extends</span> <span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">        <span class="built_in">this</span>.state =&#123;</span><br><span class="line">            num:store.getState()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//最后要想数据在视图中也改变 就要需要在 组件挂载DOM的生命周期里 重新赋值</span></span><br><span class="line">    <span class="function"><span class="title">componentDidMount</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        store.subscribe(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">                num:store.getState()</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div&gt;</span><br><span class="line">                &lt;div&gt;我是Live&lt;/div&gt;</span><br><span class="line">                &lt;div style=&#123;&#123;<span class="attr">opacity</span>:<span class="built_in">this</span>.state.opacity&#125;&#125;&gt;&#123;<span class="built_in">this</span>.props.msg&#125;&lt;/div&gt;</span><br><span class="line">                &lt;button onClick=&#123;<span class="built_in">this</span>.clearTimer&#125;&gt;不想活了<span class="number">111</span>&lt;/button&gt;</span><br><span class="line">                &lt;div&gt;&#123;<span class="built_in">this</span>.state.num&#125;&lt;/div&gt;</span><br><span class="line">                &#123;<span class="comment">/* 在这里使用store.dispatch 传递我们写好的那个函数  */</span>&#125;</span><br><span class="line">                &lt;button onClick=&#123;<span class="function">() =&gt;</span> &#123;store.dispatch(action.add(<span class="number">1</span>))&#125;&#125;&gt;年龄加&lt;/button&gt;</span><br><span class="line">                &lt;button onClick=&#123;<span class="function">() =&gt;</span> &#123;store.dispatch(action.del(<span class="number">1</span>))&#125;&#125;&gt;年龄减&lt;/button&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">            </span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="combinReducers"><a href="#combinReducers" class="headerlink" title="combinReducers"></a>combinReducers</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>用来合并多个reducer </span><br><span class="line"><span class="number">2.</span>可以理解为模块化 比如我们A组件 可以有自己的reducer B组件也可以有自己的Reducer 那么就需要创建两个Reducer 文件 </span><br><span class="line"><span class="number">3.</span>然后我们是 combinReducers  把两个Reducer都合并</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//首先我们写两个reducer 文件</span></span><br><span class="line"><span class="comment">// reducerA.js</span></span><br><span class="line"><span class="keyword">const</span> count = <span class="string">&#x27;我是reducerA&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> data = <span class="function">(<span class="params">state = count,action</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">switch</span>(action.type)&#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&#x27;edit&#x27;</span>:</span><br><span class="line">            <span class="keyword">return</span> state = action.data</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">return</span> state</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//reducerB.js</span></span><br><span class="line"><span class="keyword">const</span> str = <span class="string">&#x27;我是ReducerB&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> reducerB = <span class="function">(<span class="params">state = str,action</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">switch</span>(action.type)&#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;edit&#x27;</span>:</span><br><span class="line">        <span class="keyword">return</span> state = action.data</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">        <span class="keyword">return</span> state</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//然后我们需要在store里导入这两个rendcer 然后用combinReducers 来合并 </span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> store = createStore(combineReducers(&#123;</span><br><span class="line">    home:data,</span><br><span class="line">    list:reducerB</span><br><span class="line">&#125;),&#123;&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//这样我们在num组件中打印的时候 取值 就可以看到 home  和 list 字段挂载到了props上面</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Num</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">   </span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.props);</span><br><span class="line">        <span class="comment">//下面是打印</span></span><br><span class="line">        &#123;<span class="attr">home</span>: <span class="string">&quot;我是reducerA&quot;</span>, <span class="attr">list</span>: <span class="string">&quot;我是ReducerB&quot;</span>, <span class="attr">dispatch</span>: ƒ&#125;</span><br><span class="line">		dispatch: ƒ dispatch(action)</span><br><span class="line">		home: <span class="string">&quot;我是reducerA&quot;</span> <span class="comment">//可以看到我们的值</span></span><br><span class="line">		list: <span class="string">&quot;我是ReducerB&quot;</span></span><br><span class="line">		__proto__: <span class="built_in">Object</span></span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div&gt;&#123;<span class="built_in">this</span>.props.home&#125; ----- 显示我是reducerA &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> numState = <span class="function">(<span class="params">state</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        ...state</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(numState)(Num)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//关于触发 当我们发送actioc的时候 是要 reducer 写的 判断逻辑都一样 那么 就都会 改变值</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Plus</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    hanldeReducer = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">//这里如果 dispathc 发送的type 在reducerA 和 reducerB 里都有相应的 判断 那么都会触发判断里的函数</span></span><br><span class="line">        <span class="built_in">this</span>.props.dispatch(&#123;</span><br><span class="line">            type:<span class="string">&#x27;edit&#x27;</span>,</span><br><span class="line">            data:<span class="string">&#x27;改变&#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div&gt;</span><br><span class="line">                &lt;button onClick=&#123; <span class="built_in">this</span>.hanldeReducer&#125;&gt;reducer&lt;/button&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect()(Plus)</span><br></pre></td></tr></table></figure>
<h2 id="React-redux"><a href="#React-redux" class="headerlink" title="React-redux"></a>React-redux</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们上面已经讲过了 redux 的使用流程</span><br><span class="line">如果我们想要改变状态 就需要在组件里发送一个action函数 我们每个组件都需要引入 store 才可以做到</span><br><span class="line">如果我们组件想使用 公共的状态 就需要引入store 然后 使用store.getState()函数去获取状态</span><br><span class="line">当我们 状态值改变的时候 我们还需要使用 store.subscribe函数去监听你状态的改变 然后把组件的值赋值给我们视图上的值</span><br><span class="line">最后组件注销的时候 要取消我们的监听</span><br><span class="line"><span class="number">2.</span>react-redux 就是一个中间的代理执行者 他会把我们上面的所有操作动作都帮你处理了</span><br></pre></td></tr></table></figure>
<h3 id="react-redux两个重要的概念"><a href="#react-redux两个重要的概念" class="headerlink" title="react-redux两个重要的概念"></a>react-redux两个重要的概念</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>Provider 是一个组件 能够让你的整个app都能获取到store中的数据 一般放在根组件中</span><br><span class="line"><span class="number">2.</span>connect 是一个方法 能够使组件根store 来进行关联 用来包装 使用公共数据的组件</span><br></pre></td></tr></table></figure>
<h3 id="Provider-组件"><a href="#Provider-组件" class="headerlink" title="Provider 组件"></a>Provider 组件</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>Provider 包裹在根组件最外层 是所有组件都可以拿到state</span><br><span class="line"><span class="number">2.</span>Provider 接收store 作为 props 然后通过context 向下传递 这样react 中任何组件都可以通过context 获取到store</span><br></pre></td></tr></table></figure>
<h3 id="connect-方法"><a href="#connect-方法" class="headerlink" title="connect 方法"></a>connect 方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>被Provider 包裹的内部组件 并且把我们写好的reduc的store传入进去 要想使用 state中的数据 就必须要connect 进行一层包裹封装 </span><br><span class="line"><span class="number">2.</span>主要就是方便我们组件内部能够获取到 store中的state</span><br><span class="line"><span class="number">3.</span>connect 方法 是一个柯里化函数 第一个函数里面有两个参数 都是函数类型 第二个函数里面是你需要包装的组件</span><br><span class="line"><span class="number">4.</span>connect 的第一个函数里的第一个参数 是你需要使用数据而传递的函数 第二个参数 是你需要改变数据而传递的函数</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>如下图所示 我们在点击底部tab的时候 上面的header 的title也相应的改变</span><br><span class="line"><span class="number">2.</span>首先我们要在app根组件上 使用react-redux Provider 包裹起来 使其每个组件都会有store</span><br><span class="line"><span class="number">3.</span>然后我们需要在tab组件上 使用connect 包裹起来 因为当我们点击的时候 需要改变数据 所以我们需要在connect的第二个参数传入 对应的改变状态的函数 需要返回一个对象 其中有type</span><br><span class="line"><span class="number">4.</span>我们header是使用数据 所以也需要用connect 包裹起来 因为是使用数据 所以需要在connect 的第一个参数 去传入一个函数用来接收 数据 需要返回一个对象</span><br><span class="line"><span class="number">5.</span>以上无论怎样 我们使用connect包装完毕组件之后 你传入的函数都会挂载到当前组件的 props属性上 </span><br></pre></td></tr></table></figure>
<p><img src="6.gif" alt=""></p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在app根组件 使用Provider 进行包裹 并且把我们的store 传入进去</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./App.css&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> Layout <span class="keyword">from</span> <span class="string">&#x27;./views/Layout/Layout&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Provider&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;@/store/index.js&#x27;</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">      &#123;<span class="comment">/*使用provider 组件对根组件进行包裹 并且把store传入进去*/</span>&#125;  </span><br><span class="line">    &lt;Provider store=&#123;store&#125;&gt;</span><br><span class="line">    &lt;div className=<span class="string">&quot;App&quot;</span>&gt;</span><br><span class="line">      &lt;Layout /&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">    &lt;/Provider&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//在我们的tab组件 使用connect 函数 进行包裹 并且 传入对应改变状态的第二个参数</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; TabBar &#125; <span class="keyword">from</span> <span class="string">&#x27;antd-mobile&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;icon&#125; <span class="keyword">from</span> <span class="string">&#x27;./tabItem.js&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Home <span class="keyword">from</span> <span class="string">&#x27;../../pages/home/Home.jsx&#x27;</span></span><br><span class="line"><span class="keyword">const</span> icondata = icon()</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Tab</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>(props);</span><br><span class="line">    <span class="built_in">this</span>.state = &#123;</span><br><span class="line">      selectedTab: <span class="string">&#x27;菜谱大全&#x27;</span>,</span><br><span class="line">      hidden: <span class="literal">false</span>,</span><br><span class="line">      fullScreen: <span class="literal">true</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="title">renderContent</span>(<span class="params">pageText</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      &lt;div style=&#123;&#123; <span class="attr">backgroundColor</span>: <span class="string">&#x27;white&#x27;</span>, <span class="attr">height</span>: <span class="string">&#x27;100%&#x27;</span>, <span class="attr">textAlign</span>: <span class="string">&#x27;center&#x27;</span> &#125;&#125;&gt;</span><br><span class="line">        &lt;div style=&#123;&#123; <span class="attr">paddingTop</span>: <span class="number">60</span> &#125;&#125;&gt;Clicked “&#123;pageText&#125;” tab， show “&#123;pageText&#125;” information&lt;/div&gt;</span><br><span class="line">        &lt;a style=&#123;&#123; <span class="attr">display</span>: <span class="string">&#x27;block&#x27;</span>, <span class="attr">marginTop</span>: <span class="number">40</span>, <span class="attr">marginBottom</span>: <span class="number">20</span>, <span class="attr">color</span>: <span class="string">&#x27;#108ee9&#x27;</span> &#125;&#125;</span><br><span class="line">          onClick=&#123;<span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">            e.preventDefault();</span><br><span class="line">            <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">              hidden: !<span class="built_in">this</span>.state.hidden,</span><br><span class="line">            &#125;);</span><br><span class="line">          &#125;&#125;</span><br><span class="line">        &gt;</span><br><span class="line">          Click to show/hide tab-bar</span><br><span class="line">        &lt;/a&gt;</span><br><span class="line">        &lt;a style=&#123;&#123; <span class="attr">display</span>: <span class="string">&#x27;block&#x27;</span>, <span class="attr">marginBottom</span>: <span class="number">600</span>, <span class="attr">color</span>: <span class="string">&#x27;#108ee9&#x27;</span> &#125;&#125;</span><br><span class="line">          onClick=&#123;<span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">            e.preventDefault();</span><br><span class="line">            <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">              fullScreen: !<span class="built_in">this</span>.state.fullScreen,</span><br><span class="line">            &#125;);</span><br><span class="line">          &#125;&#125;</span><br><span class="line">        &gt;</span><br><span class="line">          Click to <span class="keyword">switch</span> fullscreen</span><br><span class="line">        &lt;/a&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>.props,<span class="string">&#x27;---------Tab&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      &lt;div style=&#123;<span class="built_in">this</span>.state.fullScreen ? &#123; <span class="attr">position</span>: <span class="string">&#x27;fixed&#x27;</span>, <span class="attr">height</span>: <span class="string">&#x27;100%&#x27;</span>, <span class="attr">width</span>: <span class="string">&#x27;100%&#x27;</span>, <span class="attr">top</span>: <span class="number">0</span> &#125; : &#123; <span class="attr">height</span>: <span class="number">400</span> &#125;&#125;&gt;</span><br><span class="line">        &lt;TabBar</span><br><span class="line">          unselectedTintColor=<span class="string">&quot;#949494&quot;</span></span><br><span class="line">          tintColor=<span class="string">&quot;#33A3F4&quot;</span></span><br><span class="line">          barTintColor=<span class="string">&quot;white&quot;</span></span><br><span class="line">          hidden=&#123;<span class="built_in">this</span>.state.hidden&#125;</span><br><span class="line">          style=&#123;&#123;<span class="attr">zIndex</span>:-<span class="number">1</span>&#125;&#125;</span><br><span class="line">        &gt;</span><br><span class="line">            &#123;icondata.map(<span class="function"><span class="params">item</span> =&gt;</span> </span><br><span class="line">                &lt;TabBar.Item</span><br><span class="line">                title=&#123;item.title&#125;</span><br><span class="line">                key=&#123;item.title&#125;</span><br><span class="line">                icon=&#123;<span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&#123;&#123;</span></span></span></span><br><span class="line"><span class="xml">                  width: &#x27;22px&#x27;,</span></span><br><span class="line"><span class="xml">                  height: &#x27;22px&#x27;,</span></span><br><span class="line"><span class="xml">                  background: `url($&#123;item.default&#125;) center center /  21px 21px no-repeat` &#125;&#125;</span></span><br><span class="line"><span class="xml">                /&gt;</span></span><br><span class="line">                &#125;</span><br><span class="line">                selectedIcon=&#123;<span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&#123;&#123;</span></span></span></span><br><span class="line"><span class="xml">                  width: &#x27;22px&#x27;,</span></span><br><span class="line"><span class="xml">                  height: &#x27;22px&#x27;,</span></span><br><span class="line"><span class="xml">                  background: `url($&#123;item.active&#125;) center center /  21px 21px no-repeat` &#125;&#125;</span></span><br><span class="line"><span class="xml">                /&gt;</span></span><br><span class="line">                &#125;</span><br><span class="line">                selected=&#123;<span class="built_in">this</span>.state.selectedTab === <span class="string">`<span class="subst">$&#123;item.title&#125;</span>`</span>&#125;</span><br><span class="line">                onPress=&#123;<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                  <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">                    selectedTab: <span class="string">`<span class="subst">$&#123;item.title&#125;</span>`</span></span><br><span class="line">                  &#125;);</span><br><span class="line">                 <span class="comment">//三、然后我们就可以使用this.props 触发这个函数 传入我们 需要的值 也就是TAB的每一项</span></span><br><span class="line">                  <span class="built_in">this</span>.props.setHeader(item.title)</span><br><span class="line">                &#125;&#125;</span><br><span class="line">              &gt;</span><br><span class="line">                &#123;item.renderContent()&#125;</span><br><span class="line">              &lt;/TabBar.Item&gt;</span><br><span class="line">            )&#125;</span><br><span class="line">        &lt;/TabBar&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//二、这是我们传入的第二个函数 首先它必须要返回一个对象 默认形参会有dipatch </span></span><br><span class="line"><span class="comment">//这样就把我们返回的对象 挂载到了我们当前实例上 props 上 </span></span><br><span class="line"><span class="keyword">const</span> setHeader = <span class="function">(<span class="params">dispach</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    setHeader:<span class="function">(<span class="params">data</span>) =&gt;</span> &#123;</span><br><span class="line">      dispach(&#123;<span class="attr">type</span>:<span class="string">&#x27;set&#x27;</span>,data&#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//一、首先要引入我们的 conect 从react-redux中 </span></span><br><span class="line"><span class="comment">//然后把我们的组件包装一下 从而 让我们传入的函数 能挂载到props上</span></span><br><span class="line"><span class="comment">//由于我们这里是 点击tab 改变公共数据 所以这里我们之传入第二个参数</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(<span class="literal">null</span>,setHeader)(Tab)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//home组件 作为使用公共主数据 我们也需要使用connect包装器来 然后传入第一个参数</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> header <span class="keyword">from</span> <span class="string">&#x27;./header.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Header</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">        <span class="comment">// this.state = &#123;</span></span><br><span class="line">        <span class="comment">//     titile:this.props.state</span></span><br><span class="line">        <span class="comment">// &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//这里我们就不必在使用监听 依然可以改变视图</span></span><br><span class="line">    <span class="comment">// componentDidMount()&#123;</span></span><br><span class="line">    <span class="comment">//     store.subscribe(() =&gt; &#123;</span></span><br><span class="line">    <span class="comment">//         this.setState(&#123;</span></span><br><span class="line">    <span class="comment">//             titile:store.getState()</span></span><br><span class="line">    <span class="comment">//         &#125;)</span></span><br><span class="line">    <span class="comment">//     &#125;)</span></span><br><span class="line">    <span class="comment">// &#125;</span></span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.props,<span class="string">&#x27;----header&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &#123;<span class="comment">/*这里就可以使用了*/</span>&#125;</span><br><span class="line">        &lt;div className=<span class="string">&#x27;title&#x27;</span>&gt;&#123;<span class="built_in">this</span>.props.state&#125;&lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//二 在connect传入的第第一个函数 这个默认的形参 就是 你的状态数据 如果有改变 就是你改变之后的状态数据  </span></span><br><span class="line"><span class="comment">//并且 把这个函数的返回值 挂载到了我们的props上 我们就可以通过this.props去使用了</span></span><br><span class="line"><span class="keyword">const</span> getTitle = <span class="function">(<span class="params">state</span>)  =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        state</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//一、我们的header需要使用这个公共数据 所以传入一个对象进去</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(getTitle)(Header)</span><br></pre></td></tr></table></figure>
<h3 id="多个action-发送的技巧"><a href="#多个action-发送的技巧" class="headerlink" title="多个action 发送的技巧"></a>多个action 发送的技巧</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>因为我们有的时候需要发送多个action 所以 我们会把这些都单独写在一个文件下</span><br><span class="line"><span class="number">2.</span>这个时候的发送 我们就需要引入 然后 在 connect的第二个参数中 在去调用</span><br><span class="line"><span class="number">3.</span>这个时候 我们就可以使用 bindActionCreators 去统一处理</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//我们发送多个action 的文件 counter.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> add = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        type:<span class="string">&#x27;add&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> jianjian = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        type:<span class="string">&#x27;jianjian&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//然后我们需要在发送action的组件中引入</span></span><br><span class="line"><span class="comment">//引入我们上面的写好的action文件 并重新命名</span></span><br><span class="line"><span class="keyword">import</span> * counterActions <span class="keyword">from</span> <span class="string">&#x27;./counter.js&#x27;</span></span><br><span class="line"><span class="comment">//引入 redux 的 bindActionCreators API</span></span><br><span class="line"><span class="keyword">import</span> &#123; bindActionCreators &#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">App</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        &lt;div&gt;</span><br><span class="line">            <span class="number">3.</span>这样在使用的时候 我们就可以直接用下面这种方式去调用了</span><br><span class="line">            &lt;button onClick=&#123;<span class="built_in">this</span>.props.conterActions.add()&#125;&gt;增加&lt;/button&gt;</span><br><span class="line">             &lt;button onClick=&#123;<span class="built_in">this</span>.props.conterActions.jianjian()&#125;&gt;减少&lt;/button&gt;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">2.</span>把握们引入的action(也就是我们写的add,和jianjian ) 当作参数 传入到我们的bindActionCreators 第二个参数把dispatch传入 </span><br><span class="line"><span class="keyword">const</span> mapDispatchToProps = <span class="function">(<span class="params">dispatch</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        conterActions:bindActionCreators(counterActions,dispatch)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span>  connect(<span class="literal">null</span>,mapDispatchToProps)(App)</span><br></pre></td></tr></table></figure>
<h3 id="react-redux流程图"><a href="#react-redux流程图" class="headerlink" title="react-redux流程图"></a>react-redux流程图</h3><p><img src="07.jpg" alt=""></p>
<h2 id="redux-saga"><a href="#redux-saga" class="headerlink" title="redux-saga"></a>redux-saga</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>可以理解为一个中间件函数 在redux里 中间件是运行在action发送出去 达到 reducer 之间的一段代码 可以把代码的流程变为 action -&gt; Middlewares -&gt; reducer(公共状态) 这种机制可以让我们改变数据流 实现列入 异步的action action过滤 日志输入 异常报告等功能</span><br><span class="line"><span class="number">2.</span>redux 提供了一个叫applyMiddleware的方法 可以执行我们的中间件</span><br><span class="line"><span class="number">3.</span> react-saga 就是redux 的一个中间件 可以通过正常的redux action 从主应用程序启动 暂停和取消 它可以访问完成的redux state 也能够 dispatch redux aciton</span><br><span class="line"><span class="number">4.</span>redux-sage 使用了ES6的 Generrator生成器函数 让异步流程 更加易于读取 写入和测试 通过这种方式 让异步看起来更加像标准的同步代码 </span><br></pre></td></tr></table></figure>
<h3 id="react-saga常用API"><a href="#react-saga常用API" class="headerlink" title="react-saga常用API"></a>react-saga常用API</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>createSagaMiddleware(options)创建一个Redux middleware 并将Saga 连接到redux store options参数 传递给middleware的选项列表 默认可以不用传递</span><br><span class="line"><span class="number">2.</span>middleware.run(saga,...args) 动态的运行saga 只能用于applyMiddleware阶段之后 执行Saga </span><br><span class="line"><span class="number">3.</span>我们可以间接的理解为 使用redux-saga 创建一个中间件 然后在 使用redux 的 applyMiddleware 函数注册这个saga创建按出来的中间件 然后在执行</span><br></pre></td></tr></table></figure>
<h3 id="saga的配置"><a href="#saga的配置" class="headerlink" title="saga的配置"></a>saga的配置</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//引出创建store 和 注册中间件的函数 </span></span><br><span class="line"><span class="keyword">import</span> &#123; createStore,applyMiddleware&#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"><span class="comment">//我们的reducer</span></span><br><span class="line"><span class="keyword">import</span> &#123;data&#125; <span class="keyword">from</span> <span class="string">&#x27;./reducer.js&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//导入我们的saga 最终需要使用run 去执行一下</span></span><br><span class="line"><span class="keyword">import</span> &#123;defSage&#125; <span class="keyword">from</span> <span class="string">&#x27;./sages&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//引入 我们的saga的中间件  需要用redux的 applyMiddleware 注册以下</span></span><br><span class="line"><span class="keyword">import</span> createSagaMiddleware <span class="keyword">from</span> <span class="string">&#x27;redux-saga&#x27;</span></span><br><span class="line"><span class="comment">//引入之后 开始创建我们的saga 中间件</span></span><br><span class="line"><span class="keyword">const</span> sagaMiddleware  = createSagaMiddleware()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 导出我们的store 然后把我们创建好的saga 中间件  使用redux的 applyMiddleware 函数注册</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> store = createStore(data,&#123;&#125;,applyMiddleware(sagaMiddleware))</span><br><span class="line"></span><br><span class="line"><span class="comment">//我们的saga中间件 有了 然后开始执行  </span></span><br><span class="line"><span class="comment">//我们知道 saga 主要是 action 到 store state 中间的一段代码</span></span><br><span class="line"><span class="comment">//那么执行哪段代码 就是我们上面写好的saga </span></span><br><span class="line">sagaMiddleware.run(defSage)</span><br></pre></td></tr></table></figure>
<h3 id="saga"><a href="#saga" class="headerlink" title="saga"></a>saga</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里的saga 就是我们 发送action 到 store 中间执行的那一段代码</span><br><span class="line"><span class="number">2.</span>这个 saga 要基于 生成器函数去写 </span><br><span class="line"><span class="number">3.</span>有三个监听你发送action 的api  分别是 takeEvery takeLatest throttle 辅助函数</span><br><span class="line"><span class="number">4.</span>引入的时候 需要 使用 <span class="keyword">import</span> &#123;takeEvery&#125; <span class="keyword">from</span> <span class="string">&#x27;redux-saga/effects&#x27;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//代码如下  我们 写了一个组件 分辨发送了action</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Plus</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    handletakeEvery = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">       <span class="built_in">this</span>.props.dispatch(&#123;</span><br><span class="line">           type:<span class="string">&#x27;takeEvery&#x27;</span></span><br><span class="line">       &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    handletakeLatset = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.props.dispatch(&#123;</span><br><span class="line">            type:<span class="string">&#x27;takeLatset&#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    handleThrottle = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.props.dispatch(&#123;</span><br><span class="line">            type:<span class="string">&#x27;Throttle&#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div&gt;</span><br><span class="line">                &lt;button onClick=&#123; <span class="built_in">this</span>.handletakeEvery&#125;&gt;点击发送takeEvery&lt;/button&gt;</span><br><span class="line">                &lt;button onClick=&#123;<span class="built_in">this</span>.handletakeLatset&#125;&gt;点击发送takeLatset&lt;/button&gt;</span><br><span class="line">                &lt;button onClick=&#123; <span class="built_in">this</span>.handleThrottle&#125;&gt;点击发送Throttle&lt;/button&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect()(Plus)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Plus</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="comment">//点击的时候发送action 传递了 数据</span></span><br><span class="line">    handletakeEvery = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">       <span class="built_in">this</span>.props.dispatch(&#123;</span><br><span class="line">           type:<span class="string">&#x27;takeEvery&#x27;</span>,</span><br><span class="line">           data:&#123;</span><br><span class="line">               username:<span class="string">&#x27;wangzheng&#x27;</span>,</span><br><span class="line">               password:<span class="number">1111</span></span><br><span class="line">           &#125;</span><br><span class="line">       &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div&gt;</span><br><span class="line">                &lt;button onClick=&#123; <span class="built_in">this</span>.handletakeEvery&#125;&gt;点击发送takeEvery&lt;/button&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// sages 文件 下  我们可以使用它提供的api 分别去监听你发送的action  语法基于生成器函数去写 下面的这三个辅助函数都是用来监听发送action</span></span><br><span class="line"><span class="comment">//获取我们发送action 传过来的参数 需要使用select </span></span><br><span class="line"><span class="keyword">import</span> &#123;takeEvery,takeLatest,Throttle&#125; <span class="keyword">from</span> <span class="string">&#x27;redux-saga/effects&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> * <span class="title">defSage</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">yield</span> takeEvery(<span class="string">&#x27;takeEvery&#x27;</span>, <span class="function"><span class="keyword">function</span>*(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">//我们使用 select 获取到了 当发送 takeEvery action的时候 发哦是那个的数据 全部获取</span></span><br><span class="line">        <span class="keyword">const</span> state = <span class="keyword">yield</span> select()</span><br><span class="line">        <span class="comment">//我们也可以  获取指定的返回数据</span></span><br><span class="line">        <span class="comment">//const state = yield select((state) =&gt; &#123;</span></span><br><span class="line">           <span class="comment">//这里里 返回的state 我们可以通过 这种形式 去获取执行的 数据</span></span><br><span class="line">           <span class="comment">//console.log(state,&#x27;----state&#x27;)</span></span><br><span class="line">       <span class="comment">//&#125;)</span></span><br><span class="line">       <span class="built_in">console</span>.log(<span class="string">&#x27;takeEvery&#x27;</span>,state) <span class="comment">// username password</span></span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">yield</span> takeLatest(<span class="string">&#x27;takeLatset&#x27;</span>,<span class="function"><span class="keyword">function</span>*(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;takeLatset&#x27;</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">yield</span> Throttle(<span class="number">0</span>,<span class="string">&#x27;Throttle&#x27;</span>,<span class="function"><span class="keyword">function</span>*(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;Throttle&#x27;</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">takeEvery,takeLatest,Throttle 的区别 </span><br><span class="line">takeEvery 你发送了多少次的action 或者异步任务 他都会执行 </span><br><span class="line">takeLatest 不论你在一定时间内发送了多少次 action 同步或者异步  他在一定能时间内 只会自行一次 类似防抖</span><br><span class="line">Throttle   你一个参数 是时间 类似节流 在规定的时间 触发的action 只 触发一定的频率</span><br><span class="line">select 用来 获取你发送action 的时候 传递的参数</span><br><span class="line">call 用来执行异步的变成代码 比如请求 </span><br><span class="line">take 用来阻塞代码的执行</span><br><span class="line">put 用来 发送action</span><br></pre></td></tr></table></figure>
<h3 id="call-和-put的使用"><a href="#call-和-put的使用" class="headerlink" title="call 和 put的使用"></a>call 和 put的使用</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//比如登录时候 我们登录成功之后 后台会给我们返回一系列的用户信息之类的 数据 这个时候 我们需要 把它存放在store里 这里 就需要用put 再次发送action</span></span><br><span class="line"><span class="keyword">import</span> &#123;takeEvery,takeLatest,Throttle,call,select，put&#125; <span class="keyword">from</span> <span class="string">&#x27;redux-saga/effects&#x27;</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> * <span class="title">defSage</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">yield</span> takeEvery(<span class="string">&#x27;takeEvery&#x27;</span>, <span class="function"><span class="keyword">function</span>*(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">//这里是获取到了 你发送action传递的数据</span></span><br><span class="line">       <span class="keyword">const</span> state = <span class="keyword">yield</span> select()</span><br><span class="line">       <span class="comment">//然后使用call 去发送请求  第一个参数是你要请求后台的时候执行的参数 第二个参数 是你的ip地址 第三个参数 是你要传递的数据</span></span><br><span class="line">       <span class="keyword">const</span> res = call(axios.post,<span class="string">&#x27;http://127.0.0.1/login&#x27;</span>,&#123;</span><br><span class="line">           ...state </span><br><span class="line">       &#125;)</span><br><span class="line">       <span class="comment">//前面请求完毕之后 后台给我们返回的数据 我们需要在发送acion 去存放的store里</span></span><br><span class="line">       <span class="keyword">yield</span> put(&#123;</span><br><span class="line">           type:<span class="string">&#x27;login_success&#x27;</span>,</span><br><span class="line">           ...res.data</span><br><span class="line">       &#125;)</span><br><span class="line">       <span class="built_in">console</span>.log(<span class="string">&#x27;takeEvery&#x27;</span>,state)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="all-使用"><a href="#all-使用" class="headerlink" title="all 使用"></a>all 使用</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们通常会有多个对应的sagas文件  比如 我们获取商品列表页面的时候 要写一个saga文件 然后写订单列表的时候 又要写一个saga 文件</span><br><span class="line"><span class="number">2.</span>所以执行的时候 我们通常会写一个saga的总文件  然后把对应模块的saga文件 引入进来 然后用all 的方法 去执行以下</span><br><span class="line"><span class="number">3.</span>最后 我们的中间件 在去跑这个saga的总文件 </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//这里用轮播图的请求举例 首先来看下我们总的saga文件   saga.js文件</span></span><br><span class="line"><span class="comment">//引入了all</span></span><br><span class="line"><span class="keyword">import</span> &#123; all &#125; <span class="keyword">from</span> <span class="string">&#x27;redux-saga/effects&#x27;</span></span><br><span class="line"><span class="comment">//引入了对应的swiper 的saga 文件 </span></span><br><span class="line"><span class="keyword">import</span> &#123;getSwiper&#125; <span class="keyword">from</span> <span class="string">&#x27;./sagas/swiperSaga.js&#x27;</span></span><br><span class="line"><span class="comment">//如果你还有对应别的模块 比如订单列表也可以引入</span></span><br><span class="line"><span class="comment">//import &#123;getList&#125; from &#x27;./sagas/list.js&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> * <span class="title">defsaga</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">//由于是多个saga文件 所以我们要使用all 去执行所有的saga文件 </span></span><br><span class="line">    <span class="keyword">yield</span> all([getSwiper()])</span><br><span class="line">    <span class="comment">//yield all([getSwiper(),getList()])</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//对应的模块的saga 文件 首先我们要是导出 然后还是要用生成器函数 切记 否则会报错 </span></span><br><span class="line"><span class="comment">//引入我们的action 常量对应的类型 因为我们在发送action的时候 是通过对应action 你发送的type 去做的判断 这里我们都写在一个文件 方便后期 维护 </span></span><br><span class="line"><span class="keyword">import</span> &#123;ACTIONS&#125; <span class="keyword">from</span> <span class="string">&#x27;@/store/actionsType.js&#x27;</span></span><br><span class="line"><span class="comment">//引入对应的副作用函数</span></span><br><span class="line"><span class="keyword">import</span> &#123;takeEvery,call,put&#125; <span class="keyword">from</span> <span class="string">&#x27;redux-saga/effects&#x27;</span></span><br><span class="line"><span class="comment">//引入 请求</span></span><br><span class="line"><span class="keyword">import</span> &#123;getSwiper <span class="keyword">as</span> swiper&#125; <span class="keyword">from</span> <span class="string">&#x27;@/api/swiper.js&#x27;</span></span><br><span class="line"><span class="comment">//开始写swiper的saga文件 </span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> * <span class="title">getSwiper</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">//这里当我们的文件页面 也就是轮播图页面发送 action的时候 使用takeEvery监听到  注意这里的回到一定要写生成器函数</span></span><br><span class="line">    <span class="keyword">yield</span> takeEvery(ACTIONS.GETSWIPER,<span class="function"><span class="keyword">function</span>*(<span class="params"></span>)</span>&#123;</span><br><span class="line">        	<span class="comment">//然后使用call 的方法 去调用我们对应的swiper的请求 拿到结果</span></span><br><span class="line">            <span class="keyword">const</span> &#123;data&#125;  = <span class="keyword">yield</span> call(swiper)</span><br><span class="line">            <span class="comment">//然后在使用put 在去发送action  对应在我们的swiper reducer里 去做对应的判断</span></span><br><span class="line">            <span class="keyword">yield</span> put(&#123;</span><br><span class="line">                type:ACTIONS.SETSWIPER,</span><br><span class="line">                data:data.swiper</span><br><span class="line">            &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//swiper 的 reducer 文件</span></span><br><span class="line"><span class="comment">// 也对应的action 的type 的常量</span></span><br><span class="line"><span class="keyword">import</span> &#123;ACTIONS&#125; <span class="keyword">from</span> <span class="string">&#x27;@/store/actionsType.js&#x27;</span></span><br><span class="line"><span class="comment">//初始默认值</span></span><br><span class="line"><span class="keyword">const</span> swiperList = [&#123;&#125;]</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> swiper = <span class="function">(<span class="params">state = swiperList,action </span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//当我们轮播图开始的时候发送生命周期 走的是ACTIONS.GETSWIPER 返回空数据 </span></span><br><span class="line">    <span class="comment">//但是我们在上面的对应swiper的saga 文件里使用takeEvery也监听到了  ACTIONS.GETSWIPER 所以去发送了请求</span></span><br><span class="line">    <span class="comment">//请求之后拿到轮播图的数据 使用put 副作用 又发送了action 然后 走到了 case ACTIONS.SETSWIPER: 把我们请求回来的数据 又返回了回去</span></span><br><span class="line">    <span class="comment">//然后我们的轮播图 又再次触发了拿state数据的函数 所以 就能成功渲染轮播图</span></span><br><span class="line">    <span class="keyword">switch</span> (action.type)&#123;</span><br><span class="line">        <span class="keyword">case</span> ACTIONS.GETSWIPER:</span><br><span class="line">        <span class="keyword">return</span> state</span><br><span class="line">        <span class="keyword">case</span> ACTIONS.SETSWIPER:</span><br><span class="line">        <span class="keyword">return</span>  <span class="built_in">Object</span>.assign([],state,action.data)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">        <span class="keyword">return</span> state</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="saga-异步请求的流程"><a href="#saga-异步请求的流程" class="headerlink" title="saga 异步请求的流程"></a>saga 异步请求的流程</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们对应的组件 会先去对应的reducer 里面取数据 第一次没有触发action</span><br><span class="line"><span class="number">2.</span>然后我们开始取请求数据 触发对应的action 第一次都返回空 此时我们的组件 还是没有取到数据</span><br><span class="line"><span class="number">3.</span>然后由于第一次发送的action 会被我们的对应的saga 监听到 所以 在这中间 我们使用了call 去发送了请求 </span><br><span class="line"><span class="number">4.</span>然后请求的结果 我们拿到 在用put 再次发送了action 这个action 对应我们的reducer的判断 找到 开始设置请求回来的数据</span><br><span class="line"><span class="number">5.</span>由于数据变了 我们的组件又再一次触发了 去数据的函数 </span><br><span class="line"><span class="number">6.</span>下面用轮播图举例</span><br></pre></td></tr></table></figure>
<p><img src="3.png" alt=""></p>
<h2 id="react-thunk"><a href="#react-thunk" class="headerlink" title="react-thunk"></a>react-thunk</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>主要是用来处理 redux的异步请求  </span><br><span class="line"><span class="number">2.</span>相比来saga来说 比较容易一些</span><br><span class="line"><span class="number">3.</span>还是需要配置中间件的形式 去使用</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//配置 这里我们一商品列表 和 通知中心举例</span></span><br><span class="line"><span class="keyword">import</span> &#123; createStore, combineReducers, compose, applyMiddleware &#125; <span class="keyword">from</span> <span class="string">&quot;redux&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> thunk <span class="keyword">from</span> <span class="string">&quot;redux-thunk&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> product <span class="keyword">from</span> <span class="string">&quot;./reducers/product&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> notice <span class="keyword">from</span> <span class="string">&quot;./reducers/notice&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> rootReducer = combineReducers(&#123;</span><br><span class="line">  product, <span class="comment">// product: product</span></span><br><span class="line">  notice</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//把我们的thunk通过如下的方式 写入进去</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> createStore(rootReducer, compose(applyMiddleware(...[thunk])));</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这里在发送的时候 就有所不同 我们还是在 每个页面文件里 使用this.prop.dispatch去发送 不过这里的传参是个函数</span></span><br><span class="line"><span class="keyword">import</span> React, &#123; useEffect, useState &#125; <span class="keyword">from</span> <span class="string">&quot;react&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; Card, Table, Button, Popconfirm &#125; <span class="keyword">from</span> <span class="string">&quot;antd&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">&quot;react-redux&quot;</span>;</span><br><span class="line"><span class="number">1.</span>引入的thunk文件</span><br><span class="line"><span class="keyword">import</span> &#123; loadProduct &#125; <span class="keyword">from</span> <span class="string">&quot;../../../store/actions/product&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; listApi, delOne, modifyOne &#125; <span class="keyword">from</span> <span class="string">&quot;../../../services/products&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; serverUrl &#125; <span class="keyword">from</span> <span class="string">&quot;../../../utils/config&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;./list.css&quot;</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">List</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  useEffect(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="number">2.</span>在最开始的时候 我们需要发送action 这个时候 由于我们配置thunk 就可以传入一个函数进去  这个函数就是我们刚刚引入的 thunk文件</span><br><span class="line">    props.dispatch(</span><br><span class="line">      loadProduct(&#123;</span><br><span class="line">        page: <span class="number">1</span></span><br><span class="line">        <span class="comment">// name: &quot;小米&quot;</span></span><br><span class="line">      &#125;)</span><br><span class="line">    );</span><br><span class="line">  &#125;, []);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> loadData = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    props.dispatch(</span><br><span class="line">      loadProduct(&#123;</span><br><span class="line">        page: page</span><br><span class="line">        <span class="comment">// name: &quot;小米&quot;</span></span><br><span class="line">      &#125;)</span><br><span class="line">    );</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">      &lt;Table</span><br><span class="line">        rowKey=<span class="string">&quot;_id&quot;</span></span><br><span class="line">        rowClassName=&#123;<span class="function"><span class="params">record</span> =&gt;</span> (record.onSale ? <span class="string">&quot;&quot;</span> : <span class="string">&quot;bg-red&quot;</span>)&#125;</span><br><span class="line">        pagination=&#123;&#123;</span><br><span class="line">          total,</span><br><span class="line">          defaultPageSize: <span class="number">2</span>,</span><br><span class="line">          onChange: <span class="function"><span class="params">p</span> =&gt;</span> &#123;</span><br><span class="line">            props.dispatch(loadProduct(&#123; <span class="attr">page</span>: p &#125;));</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;&#125;</span><br><span class="line">        columns=&#123;columns&#125;</span><br><span class="line">        bordered</span><br><span class="line">        dataSource=&#123;list&#125;</span><br><span class="line">      /&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(<span class="function"><span class="params">state</span> =&gt;</span> state.product)(List)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//store/actions/product目录下 的 函数</span></span><br><span class="line"><span class="comment">//请求的接口</span></span><br><span class="line"><span class="keyword">import</span> &#123; listApi &#125; <span class="keyword">from</span> <span class="string">&quot;../../services/products&quot;</span>;</span><br><span class="line"><span class="comment">//这个就是我们传入刚刚页面dispath的时候 传入的函数</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> loadProduct = <span class="function"><span class="params">payload</span> =&gt;</span> <span class="keyword">async</span> dispatch =&gt; &#123;</span><br><span class="line">  <span class="comment">//它会返回一个函数 函数里面去请求了接口 然后 在发送action 到 reducer</span></span><br><span class="line">  <span class="keyword">const</span> res = <span class="keyword">await</span> listApi(payload.page);</span><br><span class="line">  <span class="comment">// 当异步操作完成之后通过dispatch触发reducer改变数据</span></span><br><span class="line">  dispatch(&#123;</span><br><span class="line">    type: <span class="string">&quot;PRODUCT_LOADED&quot;</span>,</span><br><span class="line">    payload: &#123; ...res, <span class="attr">page</span>: payload.page &#125;</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="react-thunk-多个action处理"><a href="#react-thunk-多个action处理" class="headerlink" title="react-thunk 多个action处理"></a>react-thunk 多个action处理</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们在发射多个action的时候  上面已经说到 通过 redux 自带的 bindActionCreators 去发送</span><br><span class="line"><span class="number">2.</span>当我们有异步的时候 我们在action的函数  处理 就变得比价重要</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//配置 redux</span></span><br><span class="line"><span class="keyword">import</span> &#123; createStore,combineReducers,applyMiddleware&#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"><span class="comment">//引入 异步的thunk </span></span><br><span class="line"><span class="keyword">import</span> thunk <span class="keyword">from</span> <span class="string">&#x27;reduc-thunk&#x27;</span></span><br><span class="line"><span class="comment">//引入 reducer </span></span><br><span class="line"><span class="keyword">import</span> count <span class="keyword">from</span> <span class="string">&#x27;./count.js&#x27;</span>;</span><br><span class="line"><span class="comment">//合并reducer</span></span><br><span class="line"><span class="keyword">const</span> root = &#123;</span><br><span class="line">    count</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//导出仓库</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> &#123;store&#125; =  createStore(combineReducers(root),applyMiddleware(thunk))</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//配置 reducer</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> count = <span class="function">(<span class="params">state = <span class="number">0</span>,action</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">switch</span>(action.type)&#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;add&#x27;</span>:</span><br><span class="line">            <span class="keyword">return</span> state += <span class="number">1</span></span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;jj&#x27;</span>:</span><br><span class="line">            <span class="keyword">return</span> state -= <span class="number">1</span></span><br><span class="line">        <span class="keyword">default</span>:    </span><br><span class="line">            <span class="keyword">return</span> state</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//发射aciont的文件</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> add = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//这里就是请求了异步的处理 我们需要return 一个函数 然后在异步的程序中去发送action</span></span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="params">dispatch</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            dispatch(&#123;</span><br><span class="line">                type:<span class="string">&#x27;add&#x27;</span></span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;,<span class="number">1000</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> jj = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        type:<span class="string">&#x27;jj&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//异步请求的话 我们就要写在dispatch里</span></span><br><span class="line"><span class="comment">//这是我的异步请求</span></span><br><span class="line"><span class="keyword">import</span> &#123;getTable&#125; <span class="keyword">from</span> <span class="string">&#x27;@/api/table.js&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> add =  <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">async</span> (dispatch) =&gt; &#123;</span><br><span class="line">        <span class="keyword">const</span> data = <span class="keyword">await</span> getTable( &#123;</span><br><span class="line">            pageItem:<span class="number">1</span>,</span><br><span class="line">            pageSize:<span class="number">3</span></span><br><span class="line">          &#125;)</span><br><span class="line">        dispatch(&#123;</span><br><span class="line">            type:<span class="string">&#x27;add&#x27;</span>,</span><br><span class="line">            data</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> jj = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        type:<span class="string">&#x27;jj&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//需要发送action的 组件文件</span></span><br><span class="line"><span class="keyword">import</span> React, &#123; Component &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="comment">//引入 我们的API 这个api是为了让我们 一个action文件里的 多个action函数 合并成一个</span></span><br><span class="line"><span class="keyword">import</span> &#123; bindActionCreators &#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"><span class="comment">//引入我们的action文件  这个文件里 有多个action函数 我们通过*的这种形式重新命名</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> countraction <span class="keyword">from</span> <span class="string">&#x27;./actionstype&#x27;</span></span><br><span class="line"> <span class="class"><span class="keyword">class</span> <span class="title">Test</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">super</span>()</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">        测试</span><br><span class="line">        &lt;div&gt;</span><br><span class="line">          &#123;<span class="built_in">this</span>.props.state.count&#125;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">        &#123;<span class="comment">/*这样我们就能使用挂载到 admincout 的action函数了*/</span>&#125;   </span><br><span class="line">        &lt;button onClick=&#123;<span class="function">() =&gt;</span> <span class="built_in">this</span>.props.admincout.add()&#125;&gt;增加&lt;/button&gt;</span><br><span class="line">        &lt;button onClick=&#123;<span class="function">() =&gt;</span> <span class="built_in">this</span>.props.admincout.jj()&#125;&gt;减少&lt;/button&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//1.创建获取函数</span></span><br><span class="line"><span class="keyword">const</span> getcount = <span class="function">(<span class="params">state</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span>&#123;</span><br><span class="line">    state</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//2.创建发送action的函数</span></span><br><span class="line"><span class="keyword">const</span> frachAction = <span class="function">(<span class="params">dispath</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span>&#123;</span><br><span class="line">    <span class="number">3.</span>这里就是使用   bindActionCreators 把我们多个aciton都通过对象的形式挂载到了props上 </span><br><span class="line">    admincout:bindActionCreators(countraction,dispath)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">4.</span>调用 以上两个函数</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(getcount,frachAction)(Test)</span><br></pre></td></tr></table></figure>
<h2 id="小技巧"><a href="#小技巧" class="headerlink" title="小技巧"></a>小技巧</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>VSCODE中的 注释的代码折叠</span><br><span class="line"><span class="comment">//#region</span></span><br><span class="line"><span class="comment">//#endregion</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>React基础</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2019/03/22/React%E5%9F%BA%E7%A1%80/">http://example.com/2019/03/22/React%E5%9F%BA%E7%A1%80/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2019-03-22</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-06-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/React/">React</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2019/03/22/%E6%B5%8F%E8%A7%88%E5%99%A8%E5%BA%95%E5%B1%82%E6%B8%B2%E6%9F%93%E6%9C%BA%E5%88%B6/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">浏览器底层渲染机制</div></div></a></div><div class="next-post pull-right"><a href="/2019/03/21/%E5%89%8D%E7%AB%AF%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">前端性能优化</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2019/03/24/移动端美食webAPP-react/" title="webAPP 美食项目"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-24</div><div class="title">webAPP 美食项目</div></div></a></div><div><a href="/2019/03/23/React组件篇/" title="React高阶应用"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530724&t=333c5056a813a542419e5b661ebcfcd9" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-23</div><div class="title">React高阶应用</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#React%E5%9F%BA%E7%A1%80"><span class="toc-text">React基础</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%80%E4%BB%8B"><span class="toc-text">简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A8%A1%E5%9D%97%E5%8C%96%E4%B8%8E%E7%BB%84%E4%BB%B6%E5%8C%96"><span class="toc-text">模块化与组件化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E4%B8%8E-React-%E7%9A%84%E7%BB%84%E4%BB%B6"><span class="toc-text">Vue 与 React 的组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E7%9A%84%E6%90%AD%E5%BB%BA"><span class="toc-text">环境的搭建</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#webpack-dev-server"><span class="toc-text">webpack-dev-server</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#html-webpack-plugin%E6%8F%92%E4%BB%B6"><span class="toc-text">html-webpack-plugin插件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E7%9C%81%E7%95%A5%E5%90%8E%E7%BC%80%E5%90%8D"><span class="toc-text">配置省略后缀名</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%A0%B9%E7%9B%AE%E5%BD%95%E6%A0%87%E8%AF%86%E7%AC%A6"><span class="toc-text">配置根目录标识符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%89%E8%A3%85react-react-dom"><span class="toc-text">安装react react-dom</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%92%8C%E6%B8%B2%E6%9F%93"><span class="toc-text">创建和渲染</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%83%E7%B4%A0%E5%B5%8C%E5%A5%97"><span class="toc-text">元素嵌套</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8JSX%E8%AF%AD%E6%B3%95"><span class="toc-text">使用JSX语法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AEbabel"><span class="toc-text">配置babel</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JSX%E8%AF%AD%E6%B3%95"><span class="toc-text">JSX语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JSX%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-text">JSX基本语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B8%B2%E6%9F%93%E6%95%B0%E7%BB%84"><span class="toc-text">渲染数组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-text">注意事项</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React%E4%B8%AD%E7%9A%84%E6%9D%A1%E4%BB%B6%E6%B8%B2%E6%9F%93"><span class="toc-text">React中的条件渲染</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%97%E6%8E%A7%E5%92%8C%E9%9D%9E%E5%8F%97%E6%8E%A7%E7%BB%84%E4%BB%B6-%E8%A1%A8%E5%8D%95"><span class="toc-text">受控和非受控组件(表单)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%8A%B6%E6%80%81%E6%8F%90%E5%8D%87"><span class="toc-text">状态提升</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React%E5%88%9B%E5%BB%BA%E7%BB%84%E4%BB%B6"><span class="toc-text">React创建组件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BB%84%E4%BB%B6"><span class="toc-text">函数式组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BC%A0%E5%80%BC%E4%B8%8E%E6%8E%A5%E6%94%B6"><span class="toc-text">函数式组件的传值与接收</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E6%98%AF%E7%BB%84%E4%BB%B6%E9%BB%98%E8%AE%A4%E5%80%BC%E7%9A%84%E8%AE%BE%E7%BD%AE"><span class="toc-text">函数是组件默认值的设置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Class%E7%BB%84%E4%BB%B6-%E7%B1%BB%E7%BB%84%E4%BB%B6"><span class="toc-text">Class组件(类组件)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#class%E7%BB%84%E4%BB%B6%E4%BC%A0%E5%8F%82"><span class="toc-text">class组件传参</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#class%E7%BB%84%E4%BB%B6%E4%B8%AD%E7%9A%84this-state"><span class="toc-text">class组件中的this.state</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8this-setState%E4%BF%AE%E6%94%B9state%E6%95%B0%E6%8D%AE"><span class="toc-text">使用this.setState修改state数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%90%E7%BB%84%E4%BB%B6%E5%90%91%E7%88%B6%E7%BB%84%E4%BB%B6%E4%BC%A0%E9%80%92%E6%95%B0%E6%8D%AE"><span class="toc-text">子组件向父组件传递数据</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%84%E4%BB%B6%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">组件的生命周期</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8C%82%E8%BD%BD%E5%8D%B8%E8%BD%BD%E8%BF%87%E7%A8%8B"><span class="toc-text">挂载卸载过程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9B%B4%E6%96%B0%E8%BF%87%E7%A8%8B"><span class="toc-text">更新过程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B0%E5%A2%9E%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">新增的生命周期</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React%E4%B8%AD%E7%9A%84style%E7%BC%96%E5%86%99"><span class="toc-text">React中的style编写</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React%E4%BD%BF%E7%94%A8CSS"><span class="toc-text">React使用CSS</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#React%E4%B8%AD%E7%9A%84css-%E9%85%8D%E7%BD%AE%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-text">React中的css 配置作用域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#css%E6%A8%A1%E5%9D%97%E5%8C%96%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A7%84%E5%88%99"><span class="toc-text">css模块化自定义规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%AB%98%E7%89%88%E6%9C%AC%E7%9A%84css-loader%E9%85%8D%E7%BD%AE"><span class="toc-text">高版本的css-loader配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#css%E4%B8%AD%E7%9A%84%E5%85%A8%E5%B1%80"><span class="toc-text">css中的全局</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#css%E4%B8%AD%E7%9A%84%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%E6%B7%BB%E5%8A%A0%E5%A4%9A%E4%B8%AA%E7%B1%BB%E5%90%8D%E7%9A%84%E6%96%B9%E5%BC%8F"><span class="toc-text">css中的一个元素添加多个类名的方式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React%E4%B8%AD%E7%9A%84%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A"><span class="toc-text">React中的事件绑定</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React%E4%B8%AD%E7%9A%84%E6%96%87%E6%9C%AC%E6%A1%86%E7%BB%91%E5%AE%9A"><span class="toc-text">React中的文本框绑定</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#create-react-app-%E8%84%9A%E6%89%8B%E6%9E%B6%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">create react app 脚手架的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8-create-react-app-%E9%85%8D%E7%BD%AE%E8%B7%A8%E5%9F%9F%E4%BB%A3%E7%90%86"><span class="toc-text">在 create react app 配置跨域代理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#react-%E5%A4%9A%E8%A1%8C%E6%A0%87%E7%AD%BE"><span class="toc-text">react 多行标签</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React%E4%B8%AD%E7%9A%84%E8%B7%AF%E7%94%B1%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">React中的路由的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Route%E5%86%99%E6%B3%95"><span class="toc-text">Route写法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B7%AF%E7%94%B1%E5%AF%BC%E8%88%AA-Link"><span class="toc-text">路由导航 Link</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B7%AF%E7%94%B1%E5%AF%BC%E8%88%AA-NavLink"><span class="toc-text">路由导航 NavLink</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B2%BE%E5%87%86%E5%8C%B9%E9%85%8D-exact"><span class="toc-text">精准匹配  exact</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B7%AF%E7%94%B1%E7%9A%84%E5%94%AF%E4%B8%80%E6%B8%B2%E6%9F%93-Switch%E6%A0%87%E7%AD%BE"><span class="toc-text">路由的唯一渲染 Switch标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B7%AF%E7%94%B1%E7%9A%84%E9%87%8D%E5%AE%9A%E5%90%91-Redirect-%E6%A0%87%E7%AD%BE"><span class="toc-text">路由的重定向 Redirect 标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E7%BA%A7%E8%B7%AF%E7%94%B1"><span class="toc-text">二级路由</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%AB%98%E9%98%B6%E7%BB%84%E4%BB%B6-withRouter"><span class="toc-text">高阶组件  withRouter</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B%E5%BC%8F%E5%AF%BC%E8%88%AA"><span class="toc-text">编程式导航</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82"><span class="toc-text">路由传参</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#params%E4%BC%A0%E5%8F%82"><span class="toc-text">params传参</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#query%E4%BC%A0%E5%8F%82"><span class="toc-text">query传参</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Hook"><span class="toc-text">Hook</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Redux%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">Redux的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#redux%E7%9A%84%E6%95%B0%E6%8D%AE%E8%8E%B7%E5%8F%96"><span class="toc-text">redux的数据获取</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#redux%E7%9A%84%E6%95%B0%E6%8D%AE%E4%BF%AE%E6%94%B9"><span class="toc-text">redux的数据修改</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#combinReducers"><span class="toc-text">combinReducers</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React-redux"><span class="toc-text">React-redux</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#react-redux%E4%B8%A4%E4%B8%AA%E9%87%8D%E8%A6%81%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="toc-text">react-redux两个重要的概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Provider-%E7%BB%84%E4%BB%B6"><span class="toc-text">Provider 组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#connect-%E6%96%B9%E6%B3%95"><span class="toc-text">connect 方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%9A%E4%B8%AAaction-%E5%8F%91%E9%80%81%E7%9A%84%E6%8A%80%E5%B7%A7"><span class="toc-text">多个action 发送的技巧</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#react-redux%E6%B5%81%E7%A8%8B%E5%9B%BE"><span class="toc-text">react-redux流程图</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#redux-saga"><span class="toc-text">redux-saga</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#react-saga%E5%B8%B8%E7%94%A8API"><span class="toc-text">react-saga常用API</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#saga%E7%9A%84%E9%85%8D%E7%BD%AE"><span class="toc-text">saga的配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#saga"><span class="toc-text">saga</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#call-%E5%92%8C-put%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">call 和 put的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#all-%E4%BD%BF%E7%94%A8"><span class="toc-text">all 使用</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#saga-%E5%BC%82%E6%AD%A5%E8%AF%B7%E6%B1%82%E7%9A%84%E6%B5%81%E7%A8%8B"><span class="toc-text">saga 异步请求的流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#react-thunk"><span class="toc-text">react-thunk</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#react-thunk-%E5%A4%9A%E4%B8%AAaction%E5%A4%84%E7%90%86"><span class="toc-text">react-thunk 多个action处理</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B0%8F%E6%8A%80%E5%B7%A7"><span class="toc-text">小技巧</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>